1,105,254 Community Members

Pass pointer via command line possible?

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

From what I have read, command line arguments are only char* separated by space.
However feel compelled to ask if anyone knows of a way to get a pointer across to
an executable in any fashion?

The goal is to pass int *pointers for reasons of inter process communication across
different language apps.

As always I appreciate any input at all.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

It doesn't make sense to pass memory pointers between address spaces.

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

I was under the impression that a child process shares memory space.

I pass an int pointer to one app at runtime via command line and the second app modifies it. Since both apps know the address they can share the variable between them.

At least that is how I thought it to work, I have certainly done this in another scripting language, passing a whole array of pointers.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

I was under the impression that a child process shares memory space.

I pass an int pointer to one app at runtime via command line and the second app modifies it. Since both apps know the address they can share the variable between them.

At least that is how I thought it to work, I have certainly done this in another scripting language, passing a whole array of pointers.

No a child process(at least a child process in Linux) has its own address space so passing pointers from its parent will be meaningless.

Maybe you could pass along some specifics of what your trying to do.

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

Sorry, I'm working with windows > xp.

I have executable one.exe which I want to launch executable two.exe and pass it pointers to variables so they can communicate via them.

one.exe is written in a language that is not capable of carrying out the tasks C++
is, which is what two.exe is developed in, and I want two.exe to communicate these variables to one.exe via memory.

I don't think I'll have a problem with the communication as I have done it before as I said, except that time the loosely types scripting language was capable of handling the passed args as pointers. So with them and also armed with the process id of the parent exe is quite capable of sharing the passed pointers and there values.

I am pretty sure I can also achieve the same now, but not unless I can pass two.exe a pid and a pointer.

On the same subject of command line, I would also like to get the name of the parent process that spawned it, which I'm not having much luck with either :(

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

I am pretty sure I can also achieve the same now, but not unless I can pass two.exe a pid and a pointer.
(

Sounds like your accessing the parent or client address space via its pid and a pointer which sounds like your processes may not be sharing an address space.

Note: I have programmed on Windows system infrequently, so I'm ignorant of its functionality.

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

After a while researching this I think I am a little closer to my goal, but alas still not in the back of the net.

The key I think here is the type to receive the pointer, and I think my salvation might lie in intptr_t which if I read correctly is guaranteed to be able to hold a pointer.

Now the next part of my plan is a way to convert this char* representation of a pointer which will be passed in, to the actual numeric value of it.

Any ideas about that anyone?

here's hoping.

(edit)

I mean I can canvert it to its _W64 intptr_t value just by using a string streem but I'm not sure that will be enough to be used to point to
the correct address.

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

I think you should consider a few problems with your approach.

1. What if your pointer, points to dynamic memory that's been freed.
2. What if your pointer, points to memory on the stack that's been released.

Member Avatar
Ancient Dragon
Achieved Level 70
27,578 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

>>Now the next part of my plan is a way to convert this char* representation of a pointer which will be passed in, to the actual numeric value of it.

Forget that idea, it won't, and can't work for reasons previously given. Pointers can not be passed between processes because each process has its own address space. Process A can not write into process B's address space, the operating system will prevent it by producing an Access Violation type of error.

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

Hi, and thank you for the replies.

please believe me, I am not being awkward, and I respect your views and answers, but I'm
having trouble understanding why I cannot share memory space.

I'm looking at an API function on MSDN WriteProcessMemory

Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.

In a similar fashion we have ReadProcessMemory

(edit)

oops I hit submit instead of preview.

So if I pass to a process I spawn, the pid of the parent process and a pointer to an address in it, I cannot get my
head around why I would not be able to read and write to that address. I have looked inside my noobness but I'm
really having a tough time moving on here.

Member Avatar
Ancient Dragon
Achieved Level 70
27,578 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

why don't you just try it and see for yourself if it works or not.

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

It is my intention.

But my problem is converting the input to pointer

I would be passing via command line a literal string which would resemble "0x00F1DC00"
and be the string representation of a pointer.

My woe is getting it to be a pointer to the address it represents. :(

Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

As you might imagine this does not work and results in a crash

stringstream ss;
int *ptr;
ss << argv[1];
ss >> *ptr;
Member Avatar
Ancient Dragon
Achieved Level 70
27,578 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

call strtol() to convert the string to an unsigned int, then assign that to a pointer of type unsigned char*.

unsigned char* ptr;
char* p;
unsigned int x = strtol(argv[1], &p, 16);
ptr = (unsigned char*)x;

or more simply

char* p;
unsigned char* ptr = (unsigned char*)strtol(argv[1],&p, 16);
Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

Thanks, I'll have a go at that and also this.

stringstream ss;
int *iInt;
void *vVoid;
ss << argv[1];
ss >> vVoid;
iInt = (int*)vVoid;
Member Avatar
Ancient Dragon
Achieved Level 70
27,578 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

you don't need vInt if all you are going to do is pass it to the Windows function. I don't know without testing if stringstream will work correctly like that, it may just return the char* instead of converting it to an integer.

Now that I think about it

stringstream ss;
int *iInt;
unsinged int x;
ss << argv[1];
ss >> x;
iInt = (int*)x;
Member Avatar
Suzie999
Posting Pro in Training
453 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
1
 

Well I finally gave up walking down this road after not being able to
set sedebugprivileges while opening the process. :(

For the record I ended up using filemapping to the page file.

Thanks again for all of your time and help.

Suze.

(edit)

But I suppose I still managed to pass a pointer via command line so this matter is solved :)

Question Answered as of 2 Years Ago by Ancient Dragon and gerard4143
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article