Passing pointer to function using DLSYM()

M

Mike D.

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
(e-mail address removed)
 
L

Lew Pitcher

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')
 
C

CBFalconer

Mike D. said:
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 ...
 
N

nrk

Mike said:
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.
 
M

Mike D.

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?
 
J

Joona I Palaste

Mike D. said:
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 ([email protected]) ------------- 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
 
O

osmium

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 <[email protected]> wrote in message

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 ....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top