By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Passing a structure from a C program to a C DLL

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.