在C#中如何使用C结构和方法吗?提供不同的数据类型的成员,在C的例子。
enum CustomerType //C++ enum
{
Personal = 0,
Corporate = 1
};
struct Customer //C++ structure
{
_TCHAR CustomerName[50];
_TCHAR Address[250];
INT32 CustomerNumber;
CustomerType CustomerType;
bool IsHandicapped;
};
//C++ method
__declspec(dllexport) HRESULT ReadCustomer(LPCTSTR cNumber, LPVOID *pCustomer)
{
try
{
.....
.....
Customer* objCustomer = (Customer*)CoTaskMemAlloc(sizeof(Customer));
......
//Fill customer
......
*pCustomer = (LPVOID)(objCustomer);
return 0L;
}
catch(...)
{
}
}
以上的C结构和方法是在本土"Customer.dll"的 public Customer GetCustomerDetails(string cNumber, out bool bStatus)
{
int result = -1;
bStatus = false;
IntPtr dataPtr = IntPtr.Zero;
Customer objCustomer = new Customer();
try
{
result = GetCustomerDetails(cNumber, out dataPtr);
if (result == 0)
{
objCustomer = (Customer)Marshal.PtrToStructure(dataPtr, typeof(Customer));
Marshal.FreeHGlobal(dataPtr);
bStatus = true;
}
else
{
bStatus = false;
}
}
catch (Exception ex)
{
Marshal.FreeHGlobal(dataPtr);
}
return objCustomer;
}