Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,554 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,342 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



void pointer

 
Reply to this topicStart new topic

void pointer, conversion

Jingle
post 7 Aug, 2008 - 01:30 PM
Post #1


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 248


My Contributions


How can You convert a void * to a const char *?
I tried this, but it didnt work..

CODE

void kill( void * vszProc )
{
const char * szProc = (const char *)vszProc;


It didnt give me an error, until i tried to use it. Then my application crashed.
User is offlineProfile CardPM

Go to the top of the page


Tom9729
post 7 Aug, 2008 - 01:53 PM
Post #2


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,404



Thanked 7 times

Dream Kudos: 325
My Contributions


It's probably giving you a warning. If you're using GCC you can enable them with the -Wall compiler flag.
User is online!Profile CardPM

Go to the top of the page

Jingle
post 7 Aug, 2008 - 01:58 PM
Post #3


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 248


My Contributions


Visual Studio 6

It Doesnt Give me a warning either....

Im trying to use _beginthread() to create a new thread from this function, and give it the name of the process to kill.
But whenever i use the const char *, it gives me an empty string, and crashes.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 7 Aug, 2008 - 02:24 PM
Post #4


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 515



Thanked 38 times
My Contributions


And what are you passing to the thread function via. _beginthread? (meaning, what does your _beginthread and any related string code look like)?

If you're casting the corresponding argument on the other side appropriately, then you might want to make sure that the string isn't stored on the function stack (or any function stack that might not be valid by the time your thread is run). Your string should either be a literal only, or it probably should have been created with malloc/new (or possibly a global statically allocated array/string).

This post has been edited by perfectly.insane: 7 Aug, 2008 - 02:28 PM
User is offlineProfile CardPM

Go to the top of the page

Jingle
post 7 Aug, 2008 - 02:27 PM
Post #5


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 248


My Contributions


the thread starts fine... its just the void pointer conversion....

i am doing it like this:
CODE

_beginthread(kill, 0, "IXPLORE.EXE");


i even tried making a msgbox to display the const char *, to see if it would work:
CODE

MessageBox(NULL, szProc, "Test", MB_OK);


But this only gives me an empty messagebox
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 7 Aug, 2008 - 02:47 PM
Post #6


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 515



Thanked 38 times
My Contributions


In the thread, you could use something like this:

printf("vszProc = 0x%08x\n", vszProc);
printf("szProc = 0x%08x\n", szProc);

To see what the pointer addresses are. Just for diagnostic purposes. Doing this should not crash the program, as you're not dereferencing the pointers, just looking at the values. The values themselves will give you a clue as to where they're coming from. I'm guessing that the values should be approximately 0x004xxxxx or higher.

This post has been edited by perfectly.insane: 7 Aug, 2008 - 02:49 PM
User is offlineProfile CardPM

Go to the top of the page

Jingle
post 7 Aug, 2008 - 02:54 PM
Post #7


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 248


My Contributions


I Could try that... but its a Windows dialog application, not a console one..
But it should just open a console if i printf if i remember right... i'll check it out
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 7 Aug, 2008 - 03:01 PM
Post #8


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 515



Thanked 38 times
My Contributions


No, it won't (unless you have some hacked printf that calls AllocConsole). You could do this instead.

char szMessage[80];
sprintf(szMessage, "vszProc = 0x%08x", vszProc);
MessageBoxA(NULL, szMessage, "Debug", MB_OK);
sprintf(szMessage, "szProc = 0x%08x", szProc);
MessageBoxA(NULL, szMessage, "Debug", MB_OK);

This post has been edited by perfectly.insane: 7 Aug, 2008 - 03:08 PM
User is offlineProfile CardPM

Go to the top of the page

Jingle
post 7 Aug, 2008 - 03:08 PM
Post #9


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 248


My Contributions


Ok Ill Try That.
But What Do the Addresses Do For Me?
I Still want to get the Value from one into the other.
The Only Thing that i can see coming from this is that they both have an address...
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 7 Aug, 2008 - 03:12 PM
Post #10


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 515



Thanked 38 times
My Contributions


If the correct pointer value is transferred, then you should be able to read the string at that address. Looking at the pointer value, you should be able to make sure that your pointer value is at least in a realistic range (such as 0x004xxxxx, not 0x99999999, 0x00000000, etc.)

NOTE: you cannot WRITE to that memory area, because it's read only (all string literals are).
User is offlineProfile CardPM

Go to the top of the page

Jingle
post 7 Aug, 2008 - 03:16 PM
Post #11


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 248


My Contributions


Ok ill check it out...
but im a little busy right now..
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 9/7/08 11:03PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month