472,373 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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 1668
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gnanaprakash Rathinam | last post by:
Hi Expert, Is there a way to obtain assembly name in an unmanaged call? During Interop call between managed to unmanaged, I would like to know in unmanaged code about the caller of assembly file...
1
by: sunil s via DotNetMonster.com | last post by:
Hi, I've got a native C++ app which calls a 3rd parth .NET DLL using the LoadLibrary/GetProcAddress functions. This works fine when the DLL is located in the app directory, but if I move it out...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
3
by: Tony Liu | last post by:
Dear All: I create a very simple DLL by using EVC to test the problem. (The platform I am working for those program is WinCE.NET) ******************************************************* The...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
0
by: Ken Durden | last post by:
A little more info. I have a unmanaged C++ DLL, compiled without /CLR. Inside a Managed C++ DLL, I have the following class: MarshalWrapper.h ---- #ifdef _MANAGED __nogc #endif
2
by: Marek | last post by:
Hi I'm trying to call a native C++ dll using the following code (both C# and C++ code segments included). When I make the call to the method (AddTwoDoubles) that has no return value all is fine. ...
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.