Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing a structure from a C program to a C DLL

Reply
Thread Tools

Passing a structure from a C program to a C DLL

 
 
Jake Thompson
Guest
Posts: n/a
 
      01-06-2006
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

 
Reply With Quote
 
 
 
 
usenet@zevv.nl
Guest
Posts: n/a
 
      01-06-2006
Jake Thompson <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      01-06-2006
In article <(E-Mail Removed) .com>,
Jake Thompson <(E-Mail Removed)> 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.

 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      01-06-2006
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;
}
 
Reply With Quote
 
Jake Thompson
Guest
Posts: n/a
 
      01-06-2006
Thanks Grumble and (E-Mail Removed)
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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-07-2006
On 6 Jan 2006 05:52:21 -0800, "Jake Thompson"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-07-2006

"Jake Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Thanks Grumble and (E-Mail Removed)
> 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


 
Reply With Quote
 
Ico
Guest
Posts: n/a
 
      01-07-2006
Mike Wahler <(E-Mail Removed)> 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
 
Reply With Quote
 
Paleopterix Paleopterix is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-18-2011
Quote:
Originally Posted by Kenny McCormack
In article <(E-Mail Removed) .com>,
Jake Thompson <(E-Mail Removed)> 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.
What a lame and useless post. Why the hell did you bothered to write? Nothing better to do, heh? Please, next time keep your kindness and deepness for yourself and write something only if it can be of help to others, otherwise you'll just prove that you're a complete ass

PS. here I am answering to this ass with another usless post, but I cannot help it I was bothered by a similar problem to Jake's and while I was hoping for an enlightening answer I end up reading kenny's ****. So annoying.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWIG Passing Ruby structure arrays into C DLL John Reynolds Ruby 0 03-20-2008 06:54 PM
How to determine if a DLL is a COM DLL or .NET DLL Anushi ASP .Net 5 10-28-2004 01:59 PM
Why does Ruby use both tcl83.dll and tk83.dll (instead of just tk83.dll)? H. Simpson Ruby 4 08-03-2004 04:45 PM
mprapi.dll --> samlib.dll --> ntdll.dll issue. Some1 Computer Support 4 04-05-2004 02:02 AM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM



Advertisments