Calling unmarshalled DLL from C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bretta
    New Member
    • Apr 2009
    • 1

    Calling unmarshalled DLL from C#

    Hi

    I am trying to make a series of calls to an unmarshalled DLL. The header file for the DLL reads as follows:
    Code:
    BOOL WINAPI	InitUSB	(void);
    BOOL WINAPI	CloseUSB (void);
    BOOL WINAPI USBReadData(BYTE Endpoint,DWORD BytesToRead,PBYTE DataBuffer);
    BOOL WINAPI USBSendCommand(BYTE bCmd);
    BOOL WINAPI USBGetCommand (BYTE bCmd, WORD wLength, PBYTE pData);

    In my code I have defined the functions as follows:
    Code:
    		// declare function 
    		[DllImport("mirwsp", CallingConvention= CallingConvention.StdCall)]
    		private static extern Boolean InitUSB();
    		[DllImport("mirwsp", CallingConvention= CallingConvention.StdCall)]
    		private static extern Boolean CloseUSB();
    		[DllImport("mirwsp", CallingConvention= CallingConvention.StdCall)]
    		private static extern Boolean USBSendCommand(byte Cmd);
    		[DllImport("mirwsp", CallingConvention= CallingConvention.StdCall)]
    		private static extern Boolean USBGetCommand(byte Cmd, int wLen, ref IntPtr pData);
    		[DllImport("mirwsp", CallingConvention= CallingConvention.StdCall)]
    		private static extern Boolean USBReadData(byte EPNumber, long BytesToRead, ref IntPtr DataBuffer);
    InitUSB, USBSendCommand and USBReadData appear to work correctly but when I call USBGetCommand I get a "Value is no longer valid" exception when I try to call Marshal.FreeHGl obal(data);

    Code:
    		private byte[] getCommand(int len, byte command)
    		{
    			IntPtr data = Marshal.AllocHGlobal(len);
                            try {
    			        bool bSuccess = USBGetCommand(command,len,ref data);
    			
    			        byte[] returnBytes = new byte[len];
    			        Marshal.Copy(data, returnBytes, 0, len);
    			
    				Marshal.FreeHGlobal(data);
    			} catch (Exception) {
    				
    			}
    			return returnBytes;
    			
    		}
    If I ignore that error and continue running the code then the next time I make the call I get the same exception but in the previous line (Marshal.Copy(d ata, returnBytes, 0, len);) Which means I am unable to see the data returned by the function.

    Any help would be appreciated as I am pretty new to marshalling in this way.

    Thanks

    Brett
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That actually looks like how I would think to do it.
    Couple of possibilities, can you marshal something other than an IntPtr?
    Like marshal a byte[] and somehow get a ptr to that (I know it should be same thing, but it might make a difference)
    Also, have you been checking the return value from your DLL function? Make sure you only attempt to copy/marshall the bytes if successfull?
    The DLL function might be choking on the IntPtr for some reason and thus returning you a null pointer which you try to marshal?

    Comment

    Working...