Search code examples
c#mfccastingtypespinvoke

Equivalent data types of C++ in C#


I'm trying to use a MFC Dll in my C# app. It basically is a proxy for easier connection with a hardware device. I'm writing a wrapper class using P/Invoke to be able to use the methods defined in the dll.

  • I'm fairly new to P/Invoke stuff. It this going to work? I can't test it very often because I have not the hardware available to test on my dev machine.
  • Could you refer me to a page with equivalent data types of C++ in C#? What type should I use when the variable has a * next to it? like double *, ULONG *, int *

ULONG WINAPI PCXUS_Open(ULONG *hPCXUS, int boot)   
ULONG WINAPI PCXUS_WRITE( ULONG hPCXUS, int Board, int Test, int Unit, 
LPCSTR strParam, double *dblValue, double dblArrayValue1[MAX_ROW], 
double dblArrayValue2[MAX_ROW], LPSTR StrValue, int *Clipped)  

And lastly, how should I define variables with brackets in their definitions? Like:
double dblArrayValue1[MAX_ROW], double dblArrayValue1[MAX_ROW]

UPDATE: Here's the definition for the second method:

Arguments:
hPCXUS         //Your access number (see PCXUS_Open function)
Board          //Board number (0 to N)
Test           //Test number (0 to 7) (only for USPC with MUX extension)
Unit           //0 = µs ; 1 = mm ; 2 = inch
StrParam       //Pointer to parameter name ( see the list )
DblValue       //Pointer to parameter value
DblArrayValue1 //Parameter data array 1
DblArrayValue2 //Parameter data array 2
StrValue       //ASCII parameter value
Clipped        //Pointer to clip information

Solution

  • Yes, odds are decent that you can pinvoke these functions. They look like regular C functions, not instance methods of a C++ class. ULONG is uint in C#, double is plain double. The * means that a pointer to the value is passed. It is ambiguous though, it could either be an argument that's passed by reference (ref or out keyword in C#) or it could be an array.

    The first function definitely passes its 1st argument as "out uint" in C#, it returns a handle value.

    The second function is tougher. There's no decent guess whether dblValue is an array or passed by reference, its name is lousy. Start your guess at "ref double", you have to look in the C++ code to be sure. Same problem with strParam, it should be "string" if its value is passed in, StringBuilder if a value is returned. The dblArrayValue arguments are definitely arrays, double[] in C#.