interop struct types
-
I have a struct to mimic a c++ struct...I have converted types as follows: C++ C# ---- ---- WORD ushort DWORD uint char[] string the imported function passes a pointer to the struct, fills it with fun-facts and gives it back more useful than before. The only thing I currently want out of this is the char[] member of the struct and things are not working out. Here's what I have done: - I am using CharSet.Auto in the structLayout and dllImport attributes. - in the function prototype : "ref STRUCTTYPE structname" - before calling the function, initializing struct by using "new" - before calling the function, initializing the string by creating a StringBuilder buffer of appropriate size and "ToString-ing" it to the struct.string member. problem is, when I call the function and access the string, it still contains the text that I used to initialize it in the first place. I haven't done enough testing to determine the problem, but I wanted to see if you have any ideas. My first question is to verify that using a c# string to represent the c++ char[] is all cool. I think it should be. [Edit]:sigh: Almost, need [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256] because it is a fixed char array [/Edit] What I was thinking of while typing right now, is this: in c++ structs are reference variables. in c# they are value variables. if i wanted to pass a c# struct to a c++ function, i would use the ref modifyer. Since my function wants a pointer to the struct, should I be using something like "ref IntPtr structname" ???? is this my problem and my solution? [Edit]:doh: not quite, should work with "ref STRUCTTYPE structname"... if the struct were represented using a class, then would need [In,Out] instead of ref. See link in next post for details. Can't wait to get home and try it![/Edit] Tym!
-
I have a struct to mimic a c++ struct...I have converted types as follows: C++ C# ---- ---- WORD ushort DWORD uint char[] string the imported function passes a pointer to the struct, fills it with fun-facts and gives it back more useful than before. The only thing I currently want out of this is the char[] member of the struct and things are not working out. Here's what I have done: - I am using CharSet.Auto in the structLayout and dllImport attributes. - in the function prototype : "ref STRUCTTYPE structname" - before calling the function, initializing struct by using "new" - before calling the function, initializing the string by creating a StringBuilder buffer of appropriate size and "ToString-ing" it to the struct.string member. problem is, when I call the function and access the string, it still contains the text that I used to initialize it in the first place. I haven't done enough testing to determine the problem, but I wanted to see if you have any ideas. My first question is to verify that using a c# string to represent the c++ char[] is all cool. I think it should be. [Edit]:sigh: Almost, need [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256] because it is a fixed char array [/Edit] What I was thinking of while typing right now, is this: in c++ structs are reference variables. in c# they are value variables. if i wanted to pass a c# struct to a c++ function, i would use the ref modifyer. Since my function wants a pointer to the struct, should I be using something like "ref IntPtr structname" ???? is this my problem and my solution? [Edit]:doh: not quite, should work with "ref STRUCTTYPE structname"... if the struct were represented using a class, then would need [In,Out] instead of ref. See link in next post for details. Can't wait to get home and try it![/Edit] Tym!