473,799 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ULON G))+1];
} myInnerStruct;

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

I defined managed structure as below.
-----------------------------------------------------------------------
[StructLayout( LayoutKind.Sequ ential, CharSet=CharSet .Ansi)]
private struct MYSTRUCT
{
struct myInnerStruct
{
public UIntPtr anything;
[MarshalAs(Unman agedType.AnsiBS tr, SizeConst=41)]
unsafe public byte[] anything2;
}

[MarshalAs(Unman agedType.AnsiBS tr, 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("mydl l.dll", EntryPoint="foo 1", CharSet=CharSet .Ansi)]
[return : MarshalAs(Unman agedType.Struct] -------------------- 1)
public static extern MYSTRUCT* firstFoo(); ---------------------- 2)

[DllImport("mydl l.dll", EntryPoint="foo 2", 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 1747
> 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(ULON G))+1];
} myInnerStruct;

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

I defined managed structure as below.
-----------------------------------------------------------------------
[StructLayout( LayoutKind.Sequ ential, CharSet=CharSet .Ansi)]
private struct MYSTRUCT
{
struct myInnerStruct
{
public UIntPtr anything;
[MarshalAs(Unman agedType.AnsiBS tr, SizeConst=41)]
unsafe public byte[] anything2;
}

[MarshalAs(Unman agedType.AnsiBS tr, 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("mydl l.dll", EntryPoint="foo 1", CharSet=CharSet .Ansi)]
[return : MarshalAs(Unman agedType.Struct] -------------------- 1)
public static extern MYSTRUCT* firstFoo(); ---------------------- 2)
[DllImport("mydl l.dll", EntryPoint="foo 2", 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("mydl l.dll", EntryPoint="foo 2", CharSet=CharSet .Ansi)]
public static extern void secondFoo(MYSTR UCT* myStruct1);

First is ok, I think.

kuba florczyk
Nov 15 '05 #2

"Kuba Florczyk" <ch******@poczt a.onet.pl> wrote in message
news:OT******** ******@TK2MSFTN GP10.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(ULON G))+1];
} myInnerStruct;

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

I defined managed structure as below.
-----------------------------------------------------------------------
[StructLayout( LayoutKind.Sequ ential, CharSet=CharSet .Ansi)]
private struct MYSTRUCT
{
struct myInnerStruct
{
public UIntPtr anything;
[MarshalAs(Unman agedType.AnsiBS tr, SizeConst=41)]
unsafe public byte[] anything2;
}

[MarshalAs(Unman agedType.AnsiBS tr, 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("mydl l.dll", EntryPoint="foo 1", CharSet=CharSet .Ansi)]
[return : MarshalAs(Unman agedType.Struct] -------------------- 1) public static extern MYSTRUCT* tFoo(); ----------------------
2)

[DllImport("mydl l.dll", EntryPoint="foo 2", 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("mydl l.dll", EntryPoint="foo 2", CharSet=CharSet .Ansi)]
public static extern void secondFoo(MYSTR UCT* 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("mydl l.dll", EntryPoint="foo 2", CharSet=CharSet .Ansi)]
public unsafe static extern void secondFoo(MYSTR UCT* myStruct1);

so, I retried like this
[DllImport("mydl l.dll", EntryPoint="foo 2", 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
2032
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 name? Thanks, GP.
1
3220
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 to it's own directory, then I get a FileNotFoundException. I've tried manipulating the app.exe.config file's <codebase> parameter, but this doesn't seem to have any effect, possibly because the DLL does not have a strong name. The only thing...
24
7699
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 Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
3
2108
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 header looks like: #ifdef TESTDLL_EXPORTS #define TESTDLL_API __declspec(dllexport)
2
9507
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 to do that? Thanks. Tsung-Yu
13
4151
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 sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
0
1461
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
2518
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. When I try and call a function that returns a double (AddTwoDoubles2) I get the following message: PInvokeStackImbalance was detected Message: A call to PInvoke function 'tempModule!<Module>::AddTwoDoubles2' has unbalanced the stack. This is...
3
16058
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
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.