How can I call a function of unmanaged dll ?

H

hykim

I want to call a unmanaged dll's function returning some STRUCT's pointer.

the next is definition of a STRUCT.
-----------------------------------------------------------------------
typedef struct myStruct{
struct {
UINT_PTR anything;
UCHAR anything2[(10*sizeof(ULONG))+1];
} myInnerStruct;

UCHAR anything3[(6*sizeof(ULONG))+1];
ULONG anything4;
} MYSTRUCT, * MYSTRUCT;
-----------------------------------------------------------------------

I defined managed structure as below.
-----------------------------------------------------------------------
[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct MYSTRUCT
{
struct myInnerStruct
{
public UIntPtr anything;
[MarshalAs(UnmanagedType.AnsiBStr, SizeConst=41)]
unsafe public byte[] anything2;
}

[MarshalAs(UnmanagedType.AnsiBStr, SizeConst=25)]
byte anything3;
int anything4;
}
-----------------------------------------------------------------------

the next is definition of funtions inclued in unmanaged dll.
-----------------------------------------------------------------------
MYSTRUCT* foo1();
void foo2(MYSTRUCT* paStruct);
-----------------------------------------------------------------------

and, the next is my code for marshaling that funtions.
-----------------------------------------------------------------------
[DllImport("mydll.dll", EntryPoint="foo1", CharSet=CharSet.Ansi)]
[return : MarshalAs(UnmanagedType.Struct] -------------------- 1)
public static extern MYSTRUCT* firstFoo(); ---------------------- 2)

[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public static extern void secondFoo(ref MYSTRUCT myStruct1); ----- 3)
-----------------------------------------------------------------------

but, they are not work correctly. What is worse, It raise compile errors
meaning that
can't get variable address or size of managed types.


How can I solve this problem??

Plz. Help Me!! =.=;
 
K

Kuba Florczyk

I want to call a unmanaged dll's function returning some STRUCT's pointer.
the next is definition of a STRUCT.
-----------------------------------------------------------------------
typedef struct myStruct{
struct {
UINT_PTR anything;
UCHAR anything2[(10*sizeof(ULONG))+1];
} myInnerStruct;

UCHAR anything3[(6*sizeof(ULONG))+1];
ULONG anything4;
} MYSTRUCT, * MYSTRUCT;
-----------------------------------------------------------------------

I defined managed structure as below.
-----------------------------------------------------------------------
[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct MYSTRUCT
{
struct myInnerStruct
{
public UIntPtr anything;
[MarshalAs(UnmanagedType.AnsiBStr, SizeConst=41)]
unsafe public byte[] anything2;
}

[MarshalAs(UnmanagedType.AnsiBStr, SizeConst=25)]
byte anything3;
int anything4;
}
-----------------------------------------------------------------------

the next is definition of funtions inclued in unmanaged dll.
-----------------------------------------------------------------------
MYSTRUCT* foo1();
void foo2(MYSTRUCT* paStruct);
-----------------------------------------------------------------------

and, the next is my code for marshaling that funtions.
-----------------------------------------------------------------------
[DllImport("mydll.dll", EntryPoint="foo1", CharSet=CharSet.Ansi)]
[return : MarshalAs(UnmanagedType.Struct] -------------------- 1)
public static extern MYSTRUCT* firstFoo(); ---------------------- 2)

[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public static extern void secondFoo(ref MYSTRUCT myStruct1); ----- 3)
-----------------------------------------------------------------------

but, they are not work correctly. What is worse, It raise compile errors
meaning that
can't get variable address or size of managed types.


How can I solve this problem??

Plz. Help Me!! =.=;

I think the second function should be like this:

[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public static extern void secondFoo(MYSTRUCT* myStruct1);

First is ok, I think.

kuba florczyk
 
H

hykim

Kuba Florczyk said:
I want to call a unmanaged dll's function returning some STRUCT's pointer.

the next is definition of a STRUCT.
-----------------------------------------------------------------------
typedef struct myStruct{
struct {
UINT_PTR anything;
UCHAR anything2[(10*sizeof(ULONG))+1];
} myInnerStruct;

UCHAR anything3[(6*sizeof(ULONG))+1];
ULONG anything4;
} MYSTRUCT, * MYSTRUCT;
-----------------------------------------------------------------------

I defined managed structure as below.
-----------------------------------------------------------------------
[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct MYSTRUCT
{
struct myInnerStruct
{
public UIntPtr anything;
[MarshalAs(UnmanagedType.AnsiBStr, SizeConst=41)]
unsafe public byte[] anything2;
}

[MarshalAs(UnmanagedType.AnsiBStr, SizeConst=25)]
byte anything3;
int anything4;
}
tFoo(); ----------------------
2)
[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public static extern void secondFoo(ref MYSTRUCT myStruct1); ----- 3)
-----------------------------------------------------------------------

but, they are not work correctly. What is worse, It raise compile errors
meaning that
can't get variable address or size of managed types.


How can I solve this problem??

Plz. Help Me!! =.=;

I think the second function should be like this:

[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public static extern void secondFoo(MYSTRUCT* myStruct1);

First is ok, I think.

kuba florczyk


thank you, kuba florczyk.... ^^*

but, I alreay tried that.
it raise compile error
"CS0208" : Cannot take the address or size of a variable of a managed type
('S')
- Even when used with the unsafe keyword, taking the address of a managed
object is not allowed.

exactly I tried like this
[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public unsafe static extern void secondFoo(MYSTRUCT* myStruct1);

so, I retried like this
[DllImport("mydll.dll", EntryPoint="foo2", CharSet=CharSet.Ansi)]
public static extern void secondFoo(ref MYSTRUCT myStruct1);

and I succeeded to compile.
after comple as a library,linked from a consumer project.
but, when storing the return value(exactly a Pointer of MYSTRUCT) of first
function,
the same error occured ( CS0208 ).

HOW CAN I STORE RETURNED POINTER FROM A FUNCTION ?? >.<;
basically, this concept is correct ? I'm a stranger C#, Marshaling, and
unmanaged dll.
 

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