473,769 Members | 3,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem passing structs to dll's

Hi!

I am confused about the abnormal behaviour of my project.

Please consider the following situation:
I have written an application in C++.NET 2003 (managed) which loads and
executes an unmanaged function from an unmanaged dll.

On the dll-side i export this function:
typedef struct {
int a;
int b;...} mystruct;

__declspec(dlle xport) int dllfn(mystruct* x);
Ok.
This all compiles (and also executes, I can test this via logfiles, etc).
BUT:
If I assign values to the given struct, they "do not arrive" in the main
(managed) application!!!

So, something like
x->a=17;
in the dll surprisingly does not set the value to 17 in the "real" struct
allociated in the main application. On the other hand, the same instruction
in the dll with
x->a=17; printf("a: %ld", x->a);
Works!!!

I do not have any idea, why I apparently do not have access to the struct I
want.
PLEASE HELP ME!

Thank you very much for your great help in advance!!!!

Kind regards!

Dec 29 '06 #1
3 2468
Another question:
Do I have to export the definition of this struct explicitly, or is it
enough to just define the same struct in both the dll and the client
application?

Thanks in advance!
Dec 29 '06 #2

"olidem" <ol****@discuss ions.microsoft. comwrote in message
news:C6******** *************** ***********@mic rosoft.com...
My general question:

Is is correct code to call a (extern dll) function with the adress of an
local structure ?
So, in the managed vc++ .net program:

in header file:

typedef struct {
int a;
int b;...} mystruct;

__declspec(dlli mport) int dllfn(mystruct* x);

an in cpp file:

main(){
mystruct foo;
int bar;

bar = dllfn(&foo);
}

Is this doable?
Remember that the dll is unmanaged and even ANSI C compatible.
Yes, that should work. You have a native struct (not ref class or value
class), so the binary layout is compatible as long as the same packing is
used (controlled with pragma pack).
>
Please answer, this is quite urgent.

Thank you very much in advance!!!

Kind regards!

olidem

Dec 29 '06 #3
Hi Ben!
I tryed your hint, using #pragma pack (show) and got in BOTH, the native and
the managed, program the same following result:

warning C4810: value of pragma pack(show) == 8

It is as if the struct would be cleaned up by the gc if the dll-function
returns.
I also allocated the struct on the heap and gave a pointer to this adress in
the heap to the dll, with the same result.
An intersting observation:
If I assign a special value to one struct member in the managed client app,

foo.a = 815;

Then this 815 is visible in the dll. But if I change this in the dll to,
let's say 816, it does not arrive in the client again. But after the
816-assignment in the dll, this variable is really set to 816, I can work
with it.
"Ben Voigt" wrote:
>
"olidem" <ol****@discuss ions.microsoft. comwrote in message
news:C6******** *************** ***********@mic rosoft.com...
My general question:

Is is correct code to call a (extern dll) function with the adress of an
local structure ?
So, in the managed vc++ .net program:

in header file:

typedef struct {
int a;
int b;...} mystruct;

__declspec(dlli mport) int dllfn(mystruct* x);

an in cpp file:

main(){
mystruct foo;
int bar;

bar = dllfn(&foo);
}

Is this doable?
Remember that the dll is unmanaged and even ANSI C compatible.

Yes, that should work. You have a native struct (not ref class or value
class), so the binary layout is compatible as long as the same packing is
used (controlled with pragma pack).

Please answer, this is quite urgent.

Thank you very much in advance!!!

Kind regards!

olidem


Dec 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4146
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API calls are needed! // using System; using System.Runtime.InteropServices;
3
1608
by: Keith M | last post by:
Hi, I am a bit of a newcomer to C# but have experience with (unmanaged) C++. Now, I have a 3rd party dll and headers. This dll is a C++ style dll that exports classes and structs. I am attempting to write a Managed C++ wrapper dll for this dll in order that it can be used from various C# modules in the overall project. The managed C++ project can 'see' the definitions for the structs that the lower functions use in the supplied...
9
2299
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then receiving it, would that change the answer to the above?
2
2522
by: Claire | last post by:
After giving up on passing nested structs to an unmanaged DLL, I planned that I'd pass a simple buffer of bytes (the same size as the struct) to the dll, convert to a memory stream and read in the fields from that. I've 2 possible DLls to use, one is emulated and the other one is the "real" one The emulated one (that I have to use on a desktop) has a function declared as follows in win32 delphi. The Delphi version of the code works fine...
8
3615
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my C# program. When I look at the reference and I double click to view it in the objec browser , I see only structs , without their members. And I don't see methods.
8
10719
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
12
3026
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
2
4359
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of structs. The array of structs are allocated by C#. If I pass the array without 'ref' it works fine on the C++ side, but after returning to C# the array struct values are all zero. If I pass with 'ref' the application crashes. If I use a class...
0
1660
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of other pointers, to structs. Horrible isn't it? I have no way of modifying the unmanaged code, I...
0
9422
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,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
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
8863
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
7403
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
6662
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();...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2811
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.