8-O fixing C# class in unsafe code block???
-
I've got some public class class Foo { public int a; public int b; ... } And I'd like to pass its instance to some unmanaged lib function, which has the same struct with int a, int b, etc... C# Foo foo = new Foo(); fixed (void* pfoo = foo) //<--------- error???? { someFunction(pfoo); } [DllImport("somelib")] static extern unsafe int someFunction(void* p); C++ typedef struct _somestr { int a; int b; int c; ... }SOMESTR; __declspec(dllexport) someFunction(SOMESRT* p) { p->a = 10; p->b = p->c; }
chesnokov
-
I've got some public class class Foo { public int a; public int b; ... } And I'd like to pass its instance to some unmanaged lib function, which has the same struct with int a, int b, etc... C# Foo foo = new Foo(); fixed (void* pfoo = foo) //<--------- error???? { someFunction(pfoo); } [DllImport("somelib")] static extern unsafe int someFunction(void* p); C++ typedef struct _somestr { int a; int b; int c; ... }SOMESTR; __declspec(dllexport) someFunction(SOMESRT* p) { p->a = 10; p->b = p->c; }
chesnokov
Hi declare your function like this : DllImport("somelib")] static extern unsafe int someFunction(IntPtr p); then use marshalling to reconstruct your C++ strucutre -> into a C# 'struct' (google it) In your DLL Use CoTaskMemAlloc function to allocate memory To free use CoTaskMemFree
-
I've got some public class class Foo { public int a; public int b; ... } And I'd like to pass its instance to some unmanaged lib function, which has the same struct with int a, int b, etc... C# Foo foo = new Foo(); fixed (void* pfoo = foo) //<--------- error???? { someFunction(pfoo); } [DllImport("somelib")] static extern unsafe int someFunction(void* p); C++ typedef struct _somestr { int a; int b; int c; ... }SOMESTR; __declspec(dllexport) someFunction(SOMESRT* p) { p->a = 10; p->b = p->c; }
chesnokov
As lisan said, you have to use a
struct
. You probably also want this attribute:[ StructLayout( LayoutKind.Sequential ) ]
struct ...Then you have to take the address of the
struct
:fixed( void* pfoo = & foo )
You can have a look at http://pinvoke.net/[^] for some examples. Nick
---------------------------------- Be excellent to each other :)
-
Hi declare your function like this : DllImport("somelib")] static extern unsafe int someFunction(IntPtr p); then use marshalling to reconstruct your C++ strucutre -> into a C# 'struct' (google it) In your DLL Use CoTaskMemAlloc function to allocate memory To free use CoTaskMemFree
?? there is a C# structure, already allocated. I need to pass it to C++ function in the dll. Please the code snippet.
chesnokov
-
As lisan said, you have to use a
struct
. You probably also want this attribute:[ StructLayout( LayoutKind.Sequential ) ]
struct ...Then you have to take the address of the
struct
:fixed( void* pfoo = & foo )
You can have a look at http://pinvoke.net/[^] for some examples. Nick
---------------------------------- Be excellent to each other :)
fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression fixed (void* pp = p) Error 1 Cannot implicitly convert type 'SomeStruct' to 'void*'
chesnokov
-
fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression fixed (void* pp = p) Error 1 Cannot implicitly convert type 'SomeStruct' to 'void*'
chesnokov
Chesnokov Yuriy wrote:
fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression
That's because foo is a local variable, so it's already in a fixed position on the stack. You can only fix objects that is in the heap. Just get the address of the struct:
void* pfoo = &foo;
Despite everything, the person most likely to be fooling you next is yourself.
-
fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression fixed (void* pp = p) Error 1 Cannot implicitly convert type 'SomeStruct' to 'void*'
chesnokov
You have to use IntPtr instead of void use Marshall.CoTaskMemAlloc method to allocate Marshall.CoTaskMemFree method to free after using your struct
-
Chesnokov Yuriy wrote:
fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression
That's because foo is a local variable, so it's already in a fixed position on the stack. You can only fix objects that is in the heap. Just get the address of the struct:
void* pfoo = &foo;
Despite everything, the person most likely to be fooling you next is yourself.
bingo! Put the struct as a class field and allocate it there. Can I allocate it in a function that it goes onto heap??
chesnokov