• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C# Question calling an unmanaged C++ DLL

puffpio

Golden Member
I am learning about data marshalling right now while doing some programming for work.

I have an unmanaged C++ DLL that I need to use in my C# project...so I am creating a wrapper dll that import all the functions I need to use

One function, lets say called myFunc takes in a struct myStruct with a String as a member function

------------------------C++------------------

struct myStruct {
char* hello;
};

void myFunc (myStruct inVariable);

-----------------------------------------------

now in my C# wrapper, I've done:

-----------------------C#--------------------

[StructLayout(LayoutKind.Sequential)]
public struct myStruct {
[MarshalAs(UnamanagedType.LPStr)] String hello;
}

[DllImport("my_dll.dll")]
public static extern void myFunc(myStruct inVariable);

------------------------------------------------


My question is that I've only seen examples where the MarshalAs attribute is used on the dll imported function parameters....since hello is inside of myStruct, is this the proper way to do it?
Will teh strings get converted to LPStr when sent hrough the imported dll?

Thanks for any help

David
 
Here's another one that has me puzzled

--------------C++---------------------

struct struct1 {
int myInt;
};

struct struct2 {
struct1* pStruct1;
};

--------------------------------

so struct2 contains a pointer to an array of struct1's..not sure how to represent that in C#

my stab at it :

-------------------------------C#--------------------

[StructLayout(LayoutKind.Sequential)]
public struct Struct1 {
int myInt;
}

[StructLayout(LayoutKind.Sequential)]
public struct Struct2 {
IntPtr pStruct1;
}

-----------------------------------------

and then get the data later or something
is there a way to explciitly do something liek a typeOf(struct1) marshalling or somethign?
 
Back
Top