Converting Clarion Procedure Declaration to C # DLLImport

How can I convert this Clarion procedure declaration to C #? This is part of a third-party DLL written in C that does not have much documentation. I have listed a prototype method in Clarion that works correctly. In C #, I'm not sure which type to use to replace *CString . I tried char[] as @DanielC, but that didn't work. I also found that Clarion long is 32 bit (thanks @ shf301).

Clarion:

 SendRequest Procedure(*CString xData,Long DataLen,Long xTimeout),Byte,Virtual 

C # (what I tried, what doesn't work):

 [DllImport("3RD_PARTY_API.dll")] private static extern long SendRequest(ref string xData, int DataLen, int xTimeout); 

When I call the SendRequest method in C #, I get a standard PInvokeStackImbalance error from VS2010 detected. I think this is a parameter type problem, not something like CharSet or EntryPoint in a DllImport declaration . I really just got stuck on how to convert *CString to a valid C # type.

+2
source share
4 answers

I have found the answer. I wish I still worked with the developer who wrote this code so that I could yell at them. They put a wrapper around the API, so the method I listed with three parameters then calls the actual API without a timeout value. PInvokeStackImbalance exception occurred due to the fact that I added an additional parameter.

Here is the real Clarion procedure, as well as the correct C # syntax for this:

Clarion:

 SendRequest Procedure(*CString xData, Long DataLen), Byte, Virtual 

WITH#:

 [DllImport("NOVA_API.dll", EntryPoint = "SendRequest")] private static extern byte SendRequest(string xData, int DataLen); 

In summarization, Clarion *CString can be converted to a .NET string type in DLLImport declarations.

Many thanks to those who answered. Here is an additional resource that I used in my search, which I found very useful: pinvoke.net

+4
source

In C #, a long always 64 bits. long in Clarion - 32 bits. Change the long parameters in the p / Invoke declaration to int .

+3
source

According to wikipedia clarion, some weird TopSpeed ​​"double quick call" can be used, where they use 4 registers for parameters instead of two. If the code is compiled using this calling convention, it will not be possible to directly call from C #.

TopSpeed ​​/ Clarion / JPI The first four integer parameters are passed to the eax, ebx, ecx, and edx registers. Floating-point parameters are passed on the floating-point stack β€” registers st0, st1, st2, st3, st4, st5 and st6. Structure parameters are always pushed onto the stack. Additional parameters are passed to the stack after the registers are exhausted. Integer values ​​are returned in eax, pointers in edx, and floating point types in st0.

If this is not the case, probably one of your parameters is not the correct size, otherwise you will get AV instead of a damaged stack error.

You can also try to establish a calling convention in dllimport. Besides "TOPSPEED", the kernel also supports stdcall and cdecl, but "TOPSPEED" is the default. Below is a link to a tutorial on interacting with vb6 and clarion.

http://pisoft.ru/verstak/insider/cw_vb.htm

+2
source

Have you tried char[] ? I'm not sure if this will work, but it's worth it. (I would post this as a comment, but I have no rights)

+1
source

All Articles