473,218 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Passing a structure from a C program to a C DLL

Hello I created a DLL that has a function that is called from my main c
program.

In my exe I first get a a pointer to the address of the function by
using GetProcAddress and on the dll side I make sure that my function
is being exported by adding a line to the .def file.

This seems to work because when I debug it recognizes the dll and once
it hits the function it goes right into the proper location of the dll.

The problem that I am having is that the function passes a structure as
part of it's three parameters. The structure appears to have all the
correct data in in while its in the main c program. However once it
goes over to the dll the structure no longer has the proper data in it.
It's almost like it was corrupted. What I find odd is that I am
passing a Tag as one of the parameters and that seems to make it over
fine. Can someone tell me what I am doing wrong? My understanding of
GetProcAddress is that once it gets a pointer to the address that it's
just like going from one function to another within the same project.
Of course I could have that misunderstood. Any help would be
appreciated. Below is a sample of my code.

C program

static GENERICCM gencm;

gencm.pszDBName = cszConnect_s;
gencm.pszApplicationName = NULL;
gencm.pszUserID = cu_dbvi__szUserName;
gencm.pszPassword = cu_dbvi__szPassword;
gencm.pszNewPassword = (strlen(cu_dbvi__szNewPassword) ?
cu_dbvi__szNewPassword : NULL);
gencm.pszProxyID = NULL;
gencm.pszProxyScope = NULL;
gencm.fSession = SIM_SS_NORMAL;
gencm.pAsyncCtl = NULL;
lstat = u_generic_cm_call(L"Logon", &gencm, &rc); <--- function that
that is setup to be the pointer to the address in the dll - The first
value is a Tag the second is the structure I am having trouble passing
and the third is a structure that should be passed back

C DLL

struct GENERICCM gencm; <---- identical to the structure that is
passed so that it can be recieved properly

u_dll_generic_cm_call(Tag, gencm, pRC) <-- function that has an
address pointed to it in the C program
if(!wcscmp(Tag,L"Logon"))
{
l_stat = SimLibLogon( gencm.pszDBName, gencm.pszApplicationName,
gencm.pszUserID, gencm.pszPassword, gencm.pszNewPassword,
gencm.pszProxyID, gencm.pszProxyScope, gencm.fSession, gencm.pAsyncCtl,
pRC );
}

As I said I am not sure If I have to do something special to pass the
data. If that is the case can someone enlighten me? Also will I run
into the same problem with the return structure?

Thanks for your help

Jake

Jan 6 '06 #1
7 2595
Jake Thompson <re***********@hotmail.com> wrote:
The structure appears to have all the correct data in in while its in
the main c program. However once it goes over to the dll the
structure no longer has the proper data in it. It's almost like it
was corrupted. What I find odd is that I am passing a Tag as one of
the parameters and that seems to make it over fine. Can someone tell
me what I am doing wrong? My understanding of GetProcAddress is that
once it gets a pointer to the address that it's just like going from
one function to another within the same project. Of course I could
have that misunderstood. Any help would be appreciated. Below is a
sample of my code.
There is a lot non-standard C in your story (dynamic loading,
GetProcAddress(), etc), but I think that this is not part of your
problem.
static GENERICCM gencm;
[ snipped a lot of struct member initialisation ]
lstat = u_generic_cm_call(L"Logon", &gencm, &rc);
Here you are not passing the structure itself, but the address of the
structure (aka, a pointer to it)
C DLL

struct GENERICCM gencm; <---- identical to the structure that is
passed so that it can be recieved properly
Why are you declaring this structure here ? You also declare a variable
with the same name in your function parameters; this is the one that
will be used inside your function, not the global one.
u_dll_generic_cm_call(Tag, gencm, pRC) <-- function that has an
address pointed to it in the C program
Does this even compile ?
if(!wcscmp(Tag,L"Logon"))
{
l_stat = SimLibLogon( gencm.pszDBName, gencm.pszApplicationName,
gencm.pszUserID, gencm.pszPassword, gencm.pszNewPassword,
gencm.pszProxyID, gencm.pszProxyScope, gencm.fSession, gencm.pAsyncCtl, pRC );
}
It seems that here you are accessing 'gencm' as it were an instance of
your struct, but you passed a *pointer* to the struct earlier.

What I'm missing in your code is a proper prototype definition of your
function, with all the proper types of the arguments. My guess is that
you somehow pass a pointer when you are calling, but you are handling
the argument as if it were a struct, not a pointer.
As I said I am not sure If I have to do something special to pass the
data. If that is the case can someone enlighten me? Also will I run
into the same problem with the return structure?


That depends on how you are returning the structure; if you return a
pointer to a structure, you will have to think about which part of your
program is responsible for allocating and freeing the data.
--
:wq
^X^Cy^K^X^C^C^C^C
Jan 6 '06 #2
In article <11**********************@f14g2000cwb.googlegroups .com>,
Jake Thompson <re***********@hotmail.com> wrote:
Hello I created a DLL that has a function that is called from my main c
program.


Allow me to be the first to say this - and I say it from the deepness of my
heart, with all the kindness and love one has come to associate with the
helpful posts you get in this newsgroup:

Not portable. Can't discuss it here. Blah, blah, blah.

Jan 6 '06 #3
Jake Thompson wrote:
As I said I am not sure If I have to do something special to pass the
data. If that is the case can someone enlighten me? Also will I
run into the same problem with the return structure?


You seem to confuse "struct" and "pointer to struct".

I'll provide a small example.

#include <stdio.h>

struct blah { int a, b, c, d; };

void print_blah(struct blah *ps)
{
printf("%d %d %d %d\n", ps->a, ps->b, ps->c, ps->d);
}

struct blah foo(struct blah s, struct blah *ps)
{
struct blah t = s, u = *ps;

t.a = 12;
ps->c = 15;
u.b = -7;
return t;
}

int main(void)
{
struct blah u = { 1, 2, 3, 4 };
struct blah v = { 5, 6, 7, 8 };
struct blah w = { 0 };
print_blah(&u);
print_blah(&v);
print_blah(&w);
w = foo(u, &v);
print_blah(&u);
print_blah(&v);
print_blah(&w);
return 0;
}
Jan 6 '06 #4
Thanks Grumble and use...@zevv.nl
your advice was helpful - I did goof and pass an address of the
structure when expecting the structure itself. Once I changed the
passed value to the structure itself it worked. I also changed the
naming convention of the dll structure from gencm to gencmdll to make
sure that it was less confusing. I apologize to the others who stated
that I posted to the wrong group but as you can see there was some
overlap so I made a judgement call. Can't say it won't happen again
but I will try to post to the proper group next time.

Jake

BTW it did compile however i did not show all the code only the
important code to save your time

Jan 6 '06 #5
On 6 Jan 2006 05:52:21 -0800, "Jake Thompson"
<re***********@hotmail.com> wrote in comp.lang.c:
Hello I created a DLL that has a function that is called from my main c
program.


[snip]

Sorry, off-topic here. C does not have DLL's, they literally do not
exist in the C language. They are a non-standard extension provided
by your compiler and operating system combination. C does not define
of know or care how they work, your platform defines how they are
supposed to work.

Ask in news:comp.os.ms-windows.programmer.win32, where the experts on
this sort of thing hang out.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 7 '06 #6

"Jake Thompson" <re***********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks Grumble and use...@zevv.nl
your advice was helpful - I did goof and pass an address of the
structure when expecting the structure itself. Once I changed the
passed value to the structure itself it worked. I also changed the
naming convention of the dll structure from gencm to gencmdll to make
sure that it was less confusing. I apologize to the others who stated
that I posted to the wrong group but as you can see there was some
overlap so I made a judgement call. Can't say it won't happen again
but I will try to post to the proper group next time.
First, you can ignore remarks by Kenny, our resident troll.

But do note that much of your posted code is indeed nonstandard.
However a couple people have taken the trouble to identify the
fact that yours was indeed a language problem. Not everyone
is always willing or able to spend the time to do so.

Jake

BTW it did compile however i did not show all the code only the
important code to save your time


The best way to save the readers' time is to present the
problem with only standard code which still conveys the
issue in question. In this case you could have created
a 'dummy' struct type to stand in for the nonstandard one
(or provided the definition of the nonstandard one --
but this isn't always feasible, due to e.g. it might
be very large, copyright issues, etc.).
-Mike
Jan 7 '06 #7
Ico
Mike Wahler <mk******@mkwahler.net> wrote:
The best way to save the readers' time is to present the problem with
only standard code which still conveys the issue in question. In this
case you could have created a 'dummy' struct type to stand in for the
nonstandard one


There's another positive effect of doing this: often I find myself
gaining insight in the cause of a problem, just at the moment I am
isolating and simplifying the offensive piece of code to show to a
collegue or posting to a newsgroup.

Ico

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 7 '06 #8

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

Similar topics

3
by: Seung-Uk Oh | last post by:
Hi, everyone! We are developing an application using both C and C++. We have defined a structure in a C program as follows: typedef struct node { struct node *next; int value; }List;
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
1
by: nickolais909 | last post by:
Hello, I have an old C++ DLL that I am wishing to use in a VB.NET program. Previously I had a VB6 program that would pass a Type to a DLL function. For example, I would pass a variable of this...
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any corrections? ...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.