Re: PInvoke Marshalling....

Tech-Archive recommends: Speed Up your PC by fixing your registry



Nicholas,

I've just been reading through the API guide, and realised that it won't
pass me back the data in a synchronous manner.

To quote:
"A command function returns immediately to the host application...
After the command is processed, by the scanner, the host application
received a Windows message indicating the command was processed. The host
applicaition provides a message handler for the ack from the connected
device."

Eeek! In .Net, is there a way to listen for messages? I've worked a little
with message maps, but that was way back and have not see this sort of basic
message handling in .net before...

Cheers.
Dan.

"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:%23Wy5GFYFIHA.5328@xxxxxxxxxxxxxxxxxxxxxxx
Daniel,

Yes, if the API function is going to write to the buffer, then passing
a StringBuilder will cause the data to be marshaled back to you correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Daniel Bass" <danREMOVEbass@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23EkQlBYFIHA.1212@xxxxxxxxxxxxxxxxxxxxxxx
Nicholas,

Excellent, thanks for the prompt reply!

pData is a buffer that I create, then pass into the function to populate.
Does the parameter as a StringBuilder marshall this data correctly?

Thanks.
Dan.

"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:ecx5T6XFIHA.280@xxxxxxxxxxxxxxxxxxxxxxx
Daniel,

Actually, it's not pretty obvious. In C++, long corresponds to a
32-bit integer, which in C#, is an int. Also, you should declare how
your strings are marshaled in the DllImport attribute. All-in-all, your
declaration should look like this:

[DllImport("Dependencies\\SNAPI.DLL", CharSet=CharSet.Ansi)]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, int max_length);

Also, if the pData parameter is not going to be written to by the API
function, you can pass it as a string.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx


"Daniel Bass" <danREMOVEbass@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:u83rn0XFIHA.1056@xxxxxxxxxxxxxxxxxxxxxxx
Greetings!

I'm trying to call this method in a c# app...

SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE
DeviceHandle, unsigned char *pData, long max_length);

So far I've got this:
[DllImport("Dependencies\\SNAPI.DLL")]
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
StringBuilder pData, long max_length);

Keep getting a PInvokeStackImbalance error and I think it's the 2nd
paramter pData because I've referenced the device handler and the long
typedef is pretty obvious.

For the 2nd paramter I've tried the following without success:
byte[]
char[]
string
StringBuilder
[MarshalAs(UnmanagedType.AnsiBStr)] string

HELP!
Thanks.
Dan.









.



Relevant Pages

  • Re: PInvoke Marshalling....
    ... pData is a buffer that I create, then pass into the function to populate. ... Does the parameter as a StringBuilder marshall this data correctly? ... public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: PInvoke Marshalling....
    ... StringBuilder will cause the data to be marshaled back to you correctly. ... pData is a buffer that I create, then pass into the function to populate. ... Does the parameter as a StringBuilder marshall this data correctly? ... public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle, ...
    (microsoft.public.dotnet.languages.csharp)
  • File IO Read
    ... I am having a an issue trying to read a text file using the File.Read command. ... StringBuilder strXml = new StringBuilder; ... UTF8Encoding temp = new UTF8Encoding; ...
    (microsoft.public.dotnet.general)