I want to use C # interop to call a function from a dll written in c. I have header files. Take a look at this:
enum CTMBeginTransactionError { CTM_BEGIN_TRX_SUCCESS = 0, CTM_BEGIN_TRX_ERROR_ALREADY_IN_PROGRESS, CTM_BEGIN_TRX_ERROR_NOT_CONNECTED }; #pragma pack(push) #pragma pack(1) struct CTMBeginTransactionResult { char * szTransactionID; enum CTMBeginTransactionError error; }; struct CTMBeginTransactionResult ctm_begin_customer_transaction(const char * szTransactionID);
How do I call ctm_begin_customer_transaction from C #. The char * constant is good for a string, but despite various attempts (looking at stackoverflow and other sites), I cannot marshal the return structure. If I define a function to return IntPtr, it works fine ...
Edit I changed the return type to IntPtr and used: CTMBeginTransactionResult structure = (CTMBeginTransactionResult) Marshal.PtrToStructure (ptr, typeof (CTMBeginTransactionResult)); but it throws an AccessViolationException
I also tried:
IntPtr ptr = Transactions.ctm_begin_customer_transaction(""); int size = 50; byte[] byteArray = new byte[size]; Marshal.Copy(ptr, byteArray, 0, size); string stringData = Encoding.ASCII.GetString(byteArray);
stringData == "70e3589b-2de0-4d1e-978d-55e22225be95 \ 0 \" \ 0 \ 0 \ a \ 0 \ 0 \ b \ b? "at this point." 70e3589b-2de0-4d1e-978d- 55e22225be95 "is the szTransactionID of the structure. Where is Enum? Is this the next byte?
c # interop pinvoke unmarshalling
Eiver
source share