Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Interop: Importing WIN32 dll functions and void *data I/O parameters

$
0
0

I am not 100% sure what could be wrong here. I spent long hours and did all online research.

Basically, I have this C# sharp class API DLL with over 100 DllImport functions from WIN32 APIENTRY DLL.   The following are two example functions I am having trouble with getting the proper I/O of passing of a byte array.

namespace wcSDK
{
        public static class wcServerAPI
	{
	////!----------------------------------------------------------------
	////! Group: wcDoor32 API
	////! BOOL APIENTRY DoorWrite(const void *data, DWORD size);
	////! Write byte to output buffer
	////! returns TRUE if data is written, otherwise see extended error
	////!----------------------------------------------------------------
	[DllImport("wcdoor32.dll", SetLastError=true)]
	public extern static bool DoorWrite(ref byte[] data, uint size);

        ////!----------------------------------------------------------------
        ////! Group: wcsrv2.dll API
        ////! BOOL APIENTRY WcReadFile(WCHANDLE h, LPVOID buffer, DWORD requested, LPDWORD read);
        ////! Read bytes from input buffer
        ////! returns TRUE if successful, see read bytes
        ////!----------------------------------------------------------------
        [DllImport("wcsrv2.dll", SetLastError=true)]
        public extern static bool WcReadFile(int h, ref byte[] buffer, uint requested, ref uint read);
    }
}

There are other similar Read/Write byte I/O functions in the wcSDK. These are the example implementations:

C/C++ working implementation for DoorWrite():

    char *buf = "hello world!";
    BOOL f = DoorWrite((BYTE *)buf, (DWORD)strlen(buf));

C/C++ working implementation for WcReadRead():

    BYTE data[1024];
    DWORD n;
    BOOL rd = WcReadFile(hFile, data, sizeof(data), &n);

C# failed implementation for DoorWrite:

    String buf = "Hello world!";
    //byte[] data = System.Text.Encoding.UTF8.GetBytes(buf); // tried this
    byte[] data = Encoding.ASCII.GetBytes(buf);
    bool b =  wcServerAPI.DoorWrite(ref data, buf.Length);


C# failed implementation for WcReadFile

    byte[] data = new byte[1024];
    //System.Byte[] data = new System.Byte[1024];
    uint n;
    bool b = wcServerAPI.WcReadFile(hFile, ref data, data.Length, ref n);

I am getting exceptions at these functions. I know I am missing something very simple. Any assistance or guidance in the right direction would be appreciated.

Thanks


Hector Santos, CTO Santronics Software, Inc. http://www.santronics.com


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>