开发者

Equivalent data types of C++ in C#

开发者 https://www.devze.com 2023-02-14 00:31 出处:网络
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 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 stu开发者_StackOverflow社区ff. 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


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#.


Yes, that should work.

For C# data types: http://msdn.microsoft.com/en-us/library/s1ax56ch(v=VS.100).aspx

For C# pointers: http://msdn.microsoft.com/en-us/library/y31yhkeb(v=VS.100).aspx

An array of doubles is double[] in C#.


I've been working with C++ bere years... Some of the most common data types in C++ are the same in C# as in C++.

  • (C++)[int] == C# [int].
  • (C++)[char] == C# [char].
  • (C++)[float] == C# [float].
  • (C++)[double] == C# [double].
  • (C++)[bool] == C# [bool].
  • (C++)[void] == C# [void].

It's important to note that C# has some data types that C++ doesn't have, such as string and decimal.

0

精彩评论

暂无评论...
验证码 换一张
取 消