Struct and DLL

P

Peter

Hello, I've a problem with P/Invoke a DLL
I get a message "not supported exception"
What is possible wrong ?

Thanks
Peter.



the DLL structure :
This function returns the track data from a user card swipe. HANDLE
IOC_GetCreditCardData(
LPCREDIT_CARD_DATA lpCreditCardData,
BOOL bClear
); ParameterslpCreditCardData [out] Long pointer to a CREDIT_CARD_DATA
structure in which the current card swipe data is returned. bClear [in]
Boolean that specifies whether the function should clear the card swipe data
from the stored buffer. Return ValuesNonzero indicates success. Zero
indicates failure.typedef struct _CREDIT_CARD_DATA {
WCHAR szTrack1Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack1Status;
WCHAR szTrack2Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack2Status;
WCHAR szTrack3Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack3Status;
} CREDIT_CARD_DATA; //note : IOC_MAX_TRACK_LEN = 128I tried to call the DLL
in C# with :

public struct MyStruct

{

public String Data_Track_1;

public uint Status_Track_1;

public String Data_Track_2;

public uint Status_Track_2;

public String Data_Track_3;

public uint Status_Track_3;

}

[DllImport("IOCtrl.DLL", EntryPoint="IOC_GetCreditCardData",
SetLastError=true,

CharSet=CharSet.Unicode)]

public static extern bool IOC_GetCreditCardData(MyStruct lpCreditCardData,
bool bClear);

private void btnGetCCData_Click(object sender, System.EventArgs e)

{

MyStruct mystr = new MyStruct();

IOC_GetCreditCardData(mystr, false);

txtTrack2.Text = mystr.Data_Track_2;

}
 
C

Chris Tacke, eMVP

A WCHAR array cannot be marshalled as a simple string. you'll ahve to make
a class with a raw byte array that you pass to the P/Invoke and have
accessors to set/get the bytes into data more friendly to your app. There
are loads of examples on doing this in the SDF source

www.opennetcf.org/sdf


--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
P

Peter

Thanks Chris,

Didn't know that it is impossible to marshal a WCHAR array.
Do you have a particular SDF code-unit in mind which have the best possible
solution for the problem ?


Peter.


Chris Tacke said:
A WCHAR array cannot be marshalled as a simple string. you'll ahve to make
a class with a raw byte array that you pass to the P/Invoke and have
accessors to set/get the bytes into data more friendly to your app. There
are loads of examples on doing this in the SDF source

www.opennetcf.org/sdf


--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Peter said:
Hello, I've a problem with P/Invoke a DLL
I get a message "not supported exception"
What is possible wrong ?

Thanks
Peter.



the DLL structure :
This function returns the track data from a user card swipe. HANDLE
IOC_GetCreditCardData(
LPCREDIT_CARD_DATA lpCreditCardData,
BOOL bClear
); ParameterslpCreditCardData [out] Long pointer to a CREDIT_CARD_DATA
structure in which the current card swipe data is returned. bClear [in]
Boolean that specifies whether the function should clear the card swipe
data
from the stored buffer. Return ValuesNonzero indicates success. Zero
indicates failure.typedef struct _CREDIT_CARD_DATA {
WCHAR szTrack1Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack1Status;
WCHAR szTrack2Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack2Status;
WCHAR szTrack3Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack3Status;
} CREDIT_CARD_DATA; //note : IOC_MAX_TRACK_LEN = 128I tried to call the
DLL
in C# with :

public struct MyStruct

{

public String Data_Track_1;

public uint Status_Track_1;

public String Data_Track_2;

public uint Status_Track_2;

public String Data_Track_3;

public uint Status_Track_3;

}

[DllImport("IOCtrl.DLL", EntryPoint="IOC_GetCreditCardData",
SetLastError=true,

CharSet=CharSet.Unicode)]

public static extern bool IOC_GetCreditCardData(MyStruct lpCreditCardData,
bool bClear);

private void btnGetCCData_Click(object sender, System.EventArgs e)

{

MyStruct mystr = new MyStruct();

IOC_GetCreditCardData(mystr, false);

txtTrack2.Text = mystr.Data_Track_2;

}
 
P

Paul G. Tobey [eMVP]

OpenNETCF.Net uses the scheme where you define an array of bytes and then
interpret various pieces of it as the elements of the 'structure'.
IP_ADAPTER_INFO does this. NDISUIO_REQUEST_NOTIFICATION does, too, and it's
fairly small, as classes in that namespace go.

Paul T.

Peter said:
Thanks Chris,

Didn't know that it is impossible to marshal a WCHAR array.
Do you have a particular SDF code-unit in mind which have the best
possible
solution for the problem ?


Peter.


Chris Tacke said:
A WCHAR array cannot be marshalled as a simple string. you'll ahve to make
a class with a raw byte array that you pass to the P/Invoke and have
accessors to set/get the bytes into data more friendly to your app.
There
are loads of examples on doing this in the SDF source

www.opennetcf.org/sdf


--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Peter said:
Hello, I've a problem with P/Invoke a DLL
I get a message "not supported exception"
What is possible wrong ?

Thanks
Peter.



the DLL structure :
This function returns the track data from a user card swipe. HANDLE
IOC_GetCreditCardData(
LPCREDIT_CARD_DATA lpCreditCardData,
BOOL bClear
); ParameterslpCreditCardData [out] Long pointer to a CREDIT_CARD_DATA
structure in which the current card swipe data is returned. bClear [in]
Boolean that specifies whether the function should clear the card swipe
data
from the stored buffer. Return ValuesNonzero indicates success. Zero
indicates failure.typedef struct _CREDIT_CARD_DATA {
WCHAR szTrack1Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack1Status;
WCHAR szTrack2Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack2Status;
WCHAR szTrack3Data[IOC_MAX_TRACK_LEN];
DWORD dwTrack3Status;
} CREDIT_CARD_DATA; //note : IOC_MAX_TRACK_LEN = 128I tried to call
the
DLL
in C# with :

public struct MyStruct

{

public String Data_Track_1;

public uint Status_Track_1;

public String Data_Track_2;

public uint Status_Track_2;

public String Data_Track_3;

public uint Status_Track_3;

}

[DllImport("IOCtrl.DLL", EntryPoint="IOC_GetCreditCardData",
SetLastError=true,

CharSet=CharSet.Unicode)]

public static extern bool IOC_GetCreditCardData(MyStruct lpCreditCardData,
bool bClear);

private void btnGetCCData_Click(object sender, System.EventArgs e)

{

MyStruct mystr = new MyStruct();

IOC_GetCreditCardData(mystr, false);

txtTrack2.Text = mystr.Data_Track_2;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top