469,581 Members | 2,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,581 developers. It's quick & easy.

How can I call a function of unmanaged dll ?

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!! =.=;
Nov 15 '05 #1
2 1570
> 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
Nov 15 '05 #2

"Kuba Florczyk" <ch******@poczta.onet.pl> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
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* 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.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gnanaprakash Rathinam | last post: by
1 post views Thread by sunil s via DotNetMonster.com | last post: by
3 posts views Thread by msnews.microsoft.com | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.