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

Passing pointer to function using DLSYM()

P: n/a
I have a problem with a dynamic library I am developing, but it is
really more of a pointer issue than anything else. Hopefully someone
here can lend me some assistance or insight into resolving this.

Ok... here goes....

I have a function that passes a pointer to a string to another
function. For example:

int FunctionA ()
{
int result;
string myString;

myString = "12345";

result = FunctionB (&myString);
}

int FunctionB (string *myString)
{
int result;
int (*dlFunction) (string);
result = (*dlFunction) (*myString);

cout << *myString << endl;
}
Now, the following function exists within the dynamic library called
by FunctionB, above.

int dlFunction (string *passedString)
{
cout << "before: " << *passedString << endl;

*passedString = *passedString + "hello";

cout << "after: " << *passedString << endl;
}
So, now here is the problem. Everything seems to works just fine.
When dlFunction is called is will display the following output:

before: 12345
after: 12345hello

This tells me that I passed the proper pointers through these
functions, since I am able to manipulate the original "12345" string
through the dynamic library call to dlFunction. However, when the
FunctionB function continues execution, and it displays the contents
of the *myString pointer, is shows the string as containing only the
orginal value of "12345."

So it appears that the dlFunction dynamic library function manipulates
the string only within it's own context, never actually altering the
original string.

How do I pass the appropriate pointers or references to allow the
dlFunction function to alter the string being pointed to with the
*passedString pointer? What am I missing/doing wrong?

Any help would be GREATLY appreciated!

Thanks,

Mike Dailey
mi*********@mtdproducts.com
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 12 Jan 2004 11:42:17 -0800, mi*********@mtdproducts.com (Mike D.) wrote:
I have a problem with a dynamic library I am developing, but it is
really more of a pointer issue than anything else. Hopefully someone
here can lend me some assistance or insight into resolving this.

Ok... here goes....

I have a function that passes a pointer to a string to another
function. For example:

int FunctionA ()
{
int result;
string myString;
myString = "12345";
OK. First thing is to show us the definition of your "string" user-defined type.
From your usage, you've put
typedef char *string;
somewhere in your code, prior to this statement. Is that correct?

result = FunctionB (&myString);
}

int FunctionB (string *myString)
{
int result;
int (*dlFunction) (string);

result = (*dlFunction) (*myString);
You left dlfunction uninitialized, so this statement isn't going to do what you
think it will do.
cout << *myString << endl;


Here, you go weird. It appears that you *don't* have a C program at all, and
thus your problem (no matter how interesting) is off topic in comp.lang.c.

While C and C++ (the language it /appears/ you are writing in) share many
constructs and philosophies, they are not identical languages. So, while (if we
ignore the C++ constructs in your code) your problem /may/ have a C solution, we
cannot assert that such a solution will apply to the C++ program you have
presented us with.

Best bet: go ask in comp.lang.c++ or one of the newsgroups dedicated to
programming on your platform.

[snip]

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 14 '05 #2

P: n/a
"Mike D." wrote:

I have a problem with a dynamic library I am developing, but it is
really more of a pointer issue than anything else. Hopefully someone
here can lend me some assistance or insight into resolving this.
Sounds like it is off topic. What is a dynamic library.

Ok... here goes....

I have a function that passes a pointer to a string to another
function. For example:

int FunctionA ()
{
int result;
string myString;
What is a string? Probably invading implementation namespace.

myString = "12345";
To do this myString must be a char*. Thus string must be a
typedef. Things are looking bad.

result = FunctionB (&myString);
I don't see any definition of FunctionB, which must receive a
char**
}

int FunctionB (string *myString)
Here it is. Why wasn't it defined before use.
{
int result;
int (*dlFunction) (string);

result = (*dlFunction) (*myString);
dlFunction is uninitialized. Nasal Demons are flying.

cout << *myString << endl;
You never defined cout, and what are all these left shifts? Maybe
this whole thing should be made into a compilable chunk, and moved
to c.l.c++, where it might be topical.
}

Now, the following function exists within the dynamic library called
by FunctionB, above.


No it doesn't. C has no dynamic libraries. I'm getting bored, so

... snip ...

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #3

P: n/a
nrk
Mike D. wrote:
I have a problem with a dynamic library I am developing, but it is
really more of a pointer issue than anything else. Hopefully someone
here can lend me some assistance or insight into resolving this.

Ok... here goes....

I have a function that passes a pointer to a string to another
function. For example:

int FunctionA ()
{
int result;
string myString;

myString = "12345";

result = FunctionB (&myString);
}

int FunctionB (string *myString)
{
int result;
int (*dlFunction) (string);
Ignoring the C++ portions of your code, and other OT stuff in your post,
look at the above declaration: dlFunction is a pointer to a function that
takes a *string* (whatever that is) and returns an int. However, in your
definition of dlFunction below, dlFunction is a function that takes a
*pointer to string* and returns int. Since you want the passed parameter
to be modified by the function and retain those modifications later, you
most certainly want to pass a *pointer to string*. Perhaps, what you want
here is:
int (*dlFunction) (string *);


result = (*dlFunction) (*myString);

and this should likely be:
result = (*dlFunction) (myString);
cout << *myString << endl;
}
Now, the following function exists within the dynamic library called
by FunctionB, above.

int dlFunction (string *passedString)
Note that this definition takes a *pointer to string* as its argument, not a
string.

-nrk.

ps: it is rather unfortunate that you gave the same name to your
pointer-to-function and function.
{
cout << "before: " << *passedString << endl;

*passedString = *passedString + "hello";

cout << "after: " << *passedString << endl;
}
So, now here is the problem. Everything seems to works just fine.
When dlFunction is called is will display the following output:

before: 12345
after: 12345hello

This tells me that I passed the proper pointers through these
functions, since I am able to manipulate the original "12345" string
through the dynamic library call to dlFunction. However, when the
FunctionB function continues execution, and it displays the contents
of the *myString pointer, is shows the string as containing only the
orginal value of "12345."

So it appears that the dlFunction dynamic library function manipulates
the string only within it's own context, never actually altering the
original string.

How do I pass the appropriate pointers or references to allow the
dlFunction function to alter the string being pointed to with the
*passedString pointer? What am I missing/doing wrong?

Any help would be GREATLY appreciated!

Thanks,

Mike Dailey
mi*********@mtdproducts.com


Nov 14 '05 #4

P: n/a
Wow... are all newsgroups so full of condesending assholes, or did I
just get lucky?

I know fully well my code has c++ as well as C in it. It was the C
portion I asked for help with, therefore it was appropriate to ask in
this forum. The fact that I posted snippets of the C++ in my app is
irrelevent. C++ AND C can and often are used together when coding an
application.

Dlsym() is a function for accessing dynamic link libraries in C or
C++, so yes-- C DOES have the capability to access dynamic link
libraries.

The code I posted was a made-up example of what I tried to do. It is
not the actual functional code, so the fact that I didn't pick the
right function names to use for this example, or that I called the
function before declaring it is irrelevent. I posted the sample code
in the hopes that someone would simply say "here is why it isn't
working. Fix this and it should function properly."

So were all of you genious' treated like morons the first time you
asked for help with a code problem, or were you all born expert
programmers?


nrk <ra*********@deadbeef.verizon.net> wrote in message news:<U2***************@nwrddc01.gnilink.net>...
Mike D. wrote:
I have a problem with a dynamic library I am developing, but it is
really more of a pointer issue than anything else. Hopefully someone
here can lend me some assistance or insight into resolving this.

Ok... here goes....

I have a function that passes a pointer to a string to another
function. For example:

int FunctionA ()
{
int result;
string myString;

myString = "12345";

result = FunctionB (&myString);
}

int FunctionB (string *myString)
{
int result;
int (*dlFunction) (string);


Ignoring the C++ portions of your code, and other OT stuff in your post,
look at the above declaration: dlFunction is a pointer to a function that
takes a *string* (whatever that is) and returns an int. However, in your
definition of dlFunction below, dlFunction is a function that takes a
*pointer to string* and returns int. Since you want the passed parameter
to be modified by the function and retain those modifications later, you
most certainly want to pass a *pointer to string*. Perhaps, what you want
here is:
int (*dlFunction) (string *);


result = (*dlFunction) (*myString);


and this should likely be:
result = (*dlFunction) (myString);
cout << *myString << endl;
}
Now, the following function exists within the dynamic library called
by FunctionB, above.

int dlFunction (string *passedString)


Note that this definition takes a *pointer to string* as its argument, not a
string.

-nrk.

ps: it is rather unfortunate that you gave the same name to your
pointer-to-function and function.
{
cout << "before: " << *passedString << endl;

*passedString = *passedString + "hello";

cout << "after: " << *passedString << endl;
}
So, now here is the problem. Everything seems to works just fine.
When dlFunction is called is will display the following output:

before: 12345
after: 12345hello

This tells me that I passed the proper pointers through these
functions, since I am able to manipulate the original "12345" string
through the dynamic library call to dlFunction. However, when the
FunctionB function continues execution, and it displays the contents
of the *myString pointer, is shows the string as containing only the
orginal value of "12345."

So it appears that the dlFunction dynamic library function manipulates
the string only within it's own context, never actually altering the
original string.

How do I pass the appropriate pointers or references to allow the
dlFunction function to alter the string being pointed to with the
*passedString pointer? What am I missing/doing wrong?

Any help would be GREATLY appreciated!

Thanks,

Mike Dailey
mi*********@mtdproducts.com

Nov 14 '05 #5

P: n/a
Mike D. <mi*********@mtdproducts.com> scribbled the following:
Wow... are all newsgroups so full of condesending assholes, or did I
just get lucky?
Do you think anyone who tells you you are wrong is a condescending
asshole?
I know fully well my code has c++ as well as C in it. It was the C
portion I asked for help with, therefore it was appropriate to ask in
this forum. The fact that I posted snippets of the C++ in my app is
irrelevent. C++ AND C can and often are used together when coding an
application.
You are correct that if you need help with the C part, you should get
it here. However interoperability with C++ is defined by C++, not by
C.
Dlsym() is a function for accessing dynamic link libraries in C or
C++, so yes-- C DOES have the capability to access dynamic link
libraries.
No it doesn't. Dlsym() is not part of the C language, it's an
implementation-specific extension.
The code I posted was a made-up example of what I tried to do. It is
not the actual functional code, so the fact that I didn't pick the
right function names to use for this example, or that I called the
function before declaring it is irrelevent. I posted the sample code
in the hopes that someone would simply say "here is why it isn't
working. Fix this and it should function properly."
If what is at fault here is Dlsym(), then we can't help you, as Dlsym()
isn't part of the C language. You want an implementation-specific
group.
So were all of you genious' treated like morons the first time you
asked for help with a code problem, or were you all born expert
programmers?


*I* was treated like a moron at the first time, yes. I got over it
pretty quickly.

PS. Please don't top-post, thanks.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
- Andy Capp
Nov 14 '05 #6

P: n/a
Mike D. writes:

[Response at end]
Wow... are all newsgroups so full of condesending assholes, or did I
just get lucky?

I know fully well my code has c++ as well as C in it. It was the C
portion I asked for help with, therefore it was appropriate to ask in
this forum. The fact that I posted snippets of the C++ in my app is
irrelevent. C++ AND C can and often are used together when coding an
application.

Dlsym() is a function for accessing dynamic link libraries in C or
C++, so yes-- C DOES have the capability to access dynamic link
libraries.

The code I posted was a made-up example of what I tried to do. It is
not the actual functional code, so the fact that I didn't pick the
right function names to use for this example, or that I called the
function before declaring it is irrelevent. I posted the sample code
in the hopes that someone would simply say "here is why it isn't
working. Fix this and it should function properly."

So were all of you genious' treated like morons the first time you
asked for help with a code problem, or were you all born expert
programmers?


nrk <ra*********@deadbeef.verizon.net> wrote in message

news:<U2***************@nwrddc01.gnilink.net>...
Mike D. wrote:
I have a problem with a dynamic library I am developing, but it is
really more of a pointer issue than anything else. Hopefully someone
here can lend me some assistance or insight into resolving this.

Ok... here goes....

I have a function that passes a pointer to a string to another
function. For example:

int FunctionA ()
{
int result;
string myString;

myString = "12345";

result = FunctionB (&myString);
}

int FunctionB (string *myString)
{
int result;
int (*dlFunction) (string);


Ignoring the C++ portions of your code, and other OT stuff in your post,
look at the above declaration: dlFunction is a pointer to a function that takes a *string* (whatever that is) and returns an int. However, in your definition of dlFunction below, dlFunction is a function that takes a
*pointer to string* and returns int. Since you want the passed parameter to be modified by the function and retain those modifications later, you
most certainly want to pass a *pointer to string*. Perhaps, what you want here is:
int (*dlFunction) (string *);


result = (*dlFunction) (*myString);


and this should likely be:
result = (*dlFunction) (myString);
cout << *myString << endl;
}
Now, the following function exists within the dynamic library called
by FunctionB, above.

int dlFunction (string *passedString)


Note that this definition takes a *pointer to string* as its argument, not a string.

-nrk.

ps: it is rather unfortunate that you gave the same name to your
pointer-to-function and function.
{
cout << "before: " << *passedString << endl;

*passedString = *passedString + "hello";

cout << "after: " << *passedString << endl;
}
So, now here is the problem. Everything seems to works just fine.
When dlFunction is called is will display the following output:

before: 12345
after: 12345hello

This tells me that I passed the proper pointers through these
functions, since I am able to manipulate the original "12345" string
through the dynamic library call to dlFunction. However, when the
FunctionB function continues execution, and it displays the contents
of the *myString pointer, is shows the string as containing only the
orginal value of "12345."

So it appears that the dlFunction dynamic library function manipulates
the string only within it's own context, never actually altering the
original string.

How do I pass the appropriate pointers or references to allow the
dlFunction function to alter the string being pointed to with the
*passedString pointer? What am I missing/doing wrong?

Any help would be GREATLY appreciated!


I make no claims to understanding your problem. But I get the *feeling*
that you expect a function to "grow" a string. Since that function doesn't
own the space the string inhabits this seems like an unrealistic
expectation.

Although the problem is not at the boundaries, one would really like a
blackboard to describe this problem ....

To get help here you will have to rephrase your question and avoid using the
dreaded words "DLL". You do not swear in church and ....
Nov 14 '05 #7

P: n/a
Mike D. <mi*********@mtdproducts.com> spoke thus:
Wow... are all newsgroups so full of condesending assholes, or did I
just get lucky? etc.


You just got lucky. Do yourself a favor and take a look at

http://www.msu.edu/~pfaffben/writing...off-topic.html
http://www.eskimo.com/~scs/C-faq/top.html

and much will be made known to you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.