473,715 Members | 5,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointer to function using DLSYM()

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*********@mtd products.com
Nov 14 '05 #1
7 5183
On 12 Jan 2004 11:42:17 -0800, mi*********@mtd products.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
"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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #3
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*********@mtd products.com


Nov 14 '05 #4
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*********@de adbeef.verizon. net> wrote in message news:<U2******* ********@nwrddc 01.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*********@mtd products.com

Nov 14 '05 #5
Mike D. <mi*********@mt dproducts.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 interoperabilit y 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.hel sinki.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
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*********@de adbeef.verizon. net> wrote in message

news:<U2******* ********@nwrddc 01.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
Mike D. <mi*********@mt dproducts.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8

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

Similar topics

58
10157
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
3
10965
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static library. I want to override this by having another function which is exactly similar in signature including name. When somebody call this function, control should automatically come to my overriddent function. Inside this function, based on...
11
4464
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
4
4658
by: dkarthik | last post by:
Hello All, I am a newbie to C++. I was trying a sample program from internet which demonstrates dynamic class loading in C++. Actually, I thought of using this design, in one of the applications I am working on. However, the compilation was through. But when ran, the program generated a SIGSEGV error. I debugged it using gdb and found that ,when the symbol returned by dlsym() was invoked, the resulting value was null.
0
1429
by: nirnimesh | last post by:
I'm intercepting a library function call using LD_PRELOAD and in-turn calling the orignial library function. Essentially, intercept.cc: void (*real_func)(void) = (void (*) (void)) dlsym(RTLD_NEXT, "_mangled_name_of_func_"); real_func(); //! Calling the original function -- SEGFAULT This is compiled as: g++ -shared -fPIC -Wall -o libintercept.so intercept.cc -ldl
8
2099
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
54
24505
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
7
3303
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
1
452
by: etienne | last post by:
Hello all, i'm experimenting a problem in trying to cast a char * variable to a pointer to a function. My problem is that I want to execute functions in threads, using the pthread_create function. The name of the functions are read in txt file at runtime. So I want to do something like char * func_to_run = scanf(the, good, args); pthread_create (thread, attr, (magic cast)funct_to_run, arg);
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9343
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9198
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.