How to Map a C++ Struct from a DLL into C#
-
Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)
-
Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)
When you have a struct of simple datatypes creating the struct should be fairly easy.
[StructLayout(LayoutKind.Seqential)]
public struct Transaction {
[MarshalAs(UnmanagedType.LPStr, SizeConst=21)] string card_number;
[MarshalAs(UnmanagedType.U4)] int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] int card_expiryyear;
}Of course this is untested code, but I believe that is correct attributes used. HTH, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
When you have a struct of simple datatypes creating the struct should be fairly easy.
[StructLayout(LayoutKind.Seqential)]
public struct Transaction {
[MarshalAs(UnmanagedType.LPStr, SizeConst=21)] string card_number;
[MarshalAs(UnmanagedType.U4)] int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] int card_expiryyear;
}Of course this is untested code, but I believe that is correct attributes used. HTH, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)
-
Hi I am using a DLL, where one of the functions need a pointer to a C/C++ struct, here is the struct : struct Transaction { char card_number[21]; int card_expirymonth; int card_expiryyear; } So I need to be able to declare the struct in C#, and pass a pointer from the C# "struct/class" to the DLL funcion. Of cause the sizeof should be the same. I hope it can be done :) Thanks Felix :)
Oops, I think there is an error in what I suggested trying.
[StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
public struct Transaction {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
[MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
}What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct. Sorry about that, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
Oops, I think there is an error in what I suggested trying.
[StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
public struct Transaction {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
[MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
}What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct. Sorry about that, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
Hi James, Thanks, I did some testing Yesterday, and it did'nt work, when I passed the struct to the DLL, it got a the pointer to "card_number", and the DLL was unable to fetch the other fields. Felix
-
Oops, I think there is an error in what I suggested trying.
[StructLayout(LayoutKind.Seqential, CharSet=CharSet.Ansi)]
public struct Transaction {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string card_number;
[MarshalAs(UnmanagedType.U4)] public int card_expirymonth;
[MarshalAs(UnmanagedType.U4)] public int card_expiryyear;
}What I told you the first time would have been correct if card_number were a pointer to a char* and not an array in the struct. Sorry about that, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
Hi James, It seems to working as a CHARM :) Thanks again :) Felix
-
Hi James, It seems to working as a CHARM :) Thanks again :) Felix
No problem, I learned a bit myself :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972