Click here to Skip to main content
15,918,668 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralOT Pin
CPallini3-Nov-09 6:50
mveCPallini3-Nov-09 6:50 
GeneralRe: OT Pin
Hamid_RT3-Nov-09 19:28
Hamid_RT3-Nov-09 19:28 
QuestionHow can add path of all files to List Ctrl those are present in folder ? Pin
Le@rner3-Nov-09 1:51
Le@rner3-Nov-09 1:51 
AnswerRe: How can add path of all files to List Ctrl those are present in folder ? Pin
Code-o-mat3-Nov-09 1:57
Code-o-mat3-Nov-09 1:57 
GeneralRe: How can add path of all files to List Ctrl those are present in folder ? Pin
iwt.dev3-Nov-09 4:34
iwt.dev3-Nov-09 4:34 
GeneralRe: How can add path of all files to List Ctrl those are present in folder ? Pin
David Crow3-Nov-09 5:11
David Crow3-Nov-09 5:11 
QuestionRe: How can add path of all files to List Ctrl those are present in folder ? Pin
David Crow3-Nov-09 3:12
David Crow3-Nov-09 3:12 
QuestionCopy code Pin
Nikola Tanev3-Nov-09 1:24
Nikola Tanev3-Nov-09 1:24 
Ok so I want to learn what is the problem in the following code:

typedef int (*foo_ptr)(void);

static int foo()
{
return 20;
}
static void after_foo(){}

void main(int argc, char **argv)
{
size_t foo_size = (LPBYTE)after_foo - (LPBYTE)foo;

foo_ptr p_foo = (foo_ptr) VirtualAlloc(0, foo_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
CopyMemory(p_foo, &foo, foo_size);

int res = (*p_foo)();
}

Now it works perfectly as this but it crashes as soon as i insert printf("something") in the foo function. so the code that fails is the following:

typedef int (*foo_ptr)(void);

static int foo()
{
// this is the only modification
printf("test to print");
return 20;
}

static void after_foo(){}

void main(int argc, char **argv)
{
size_t foo_size = (LPBYTE)after_foo - (LPBYTE)foo;

foo_ptr p_foo = (foo_ptr) VirtualAlloc(0, foo_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
CopyMemory(p_foo, &foo, foo_size);

int res = (*p_foo)();
}

What i have noticed is that it crushes each time i insert a code that access the heap memory in the foo function. If i insert a code that allocates a heap it is not a problem (example int x;). I think it is something with the static functions and their address translation (absolute vs relative)...

Any answer would be appreciated

P.S. Please note that i do not want to do some mallware code.. i mean i know how to inject the code in the address space of another process i just cant figure it out how to insert it in the address space of the same process...... I have done a data management component which i use to share data between processes (IPC). The data management component works on a File mapped memory and uses custom heap implementation over that memory (using offsets since the memory is mapped differently in each process) to store the data. Now i want to boost it to share not only data between processes but also a code. The idea is that i do not want to inject the code from another process instead to inject the code in a shared memory space then from another process to copy the function in the local memory space and execute it from there.

Example:
App1 -> store foo() in DM
App2 -> read foo() from DM
App2 -> write foo() in local memory space (with PAGE_EXECUTE_READWRITE protection flag set)
App2 -> execute foo()

* DM is already created and works fine with data
* I understand that foo must not call functions from libraries that are not loaded in App2

Uffff tooo long post... sorry Smile | :)

regards,
Nikola Tanev
AnswerRe: Copy code Pin
Nikola Tanev3-Nov-09 11:32
Nikola Tanev3-Nov-09 11:32 
GeneralRe: Copy code Pin
enhzflep3-Nov-09 11:44
enhzflep3-Nov-09 11:44 
GeneralRe: Copy code Pin
Nikola Tanev3-Nov-09 21:00
Nikola Tanev3-Nov-09 21:00 
QuestionMemory allocation problem !!! Pin
Game-point3-Nov-09 1:13
Game-point3-Nov-09 1:13 
AnswerRe: Memory allocation problem !!! Pin
PrafullaShirke273-Nov-09 1:21
professionalPrafullaShirke273-Nov-09 1:21 
GeneralRe: Memory allocation problem !!! Pin
Game-point3-Nov-09 1:28
Game-point3-Nov-09 1:28 
GeneralRe: Memory allocation problem !!! Pin
PrafullaShirke273-Nov-09 1:38
professionalPrafullaShirke273-Nov-09 1:38 
QuestionRe: Memory allocation problem !!! Pin
CPallini3-Nov-09 1:54
mveCPallini3-Nov-09 1:54 
AnswerRe: Memory allocation problem !!! Pin
PrafullaShirke273-Nov-09 2:02
professionalPrafullaShirke273-Nov-09 2:02 
GeneralRe: Memory allocation problem !!! Pin
CPallini3-Nov-09 2:04
mveCPallini3-Nov-09 2:04 
GeneralRe: Memory allocation problem !!! Pin
CPallini3-Nov-09 2:03
mveCPallini3-Nov-09 2:03 
QuestionRe: Memory allocation problem !!! Pin
David Crow3-Nov-09 3:08
David Crow3-Nov-09 3:08 
Questionhow to insert data in sqlserver 2005 using vc++6.0 Pin
eswar pothula2-Nov-09 22:56
eswar pothula2-Nov-09 22:56 
AnswerRe: how to insert data in sqlserver 2005 using vc++6.0 [modified] Pin
super2-Nov-09 23:08
professionalsuper2-Nov-09 23:08 
AnswerRe: how to insert data in sqlserver 2005 using vc++6.0 Pin
CPallini2-Nov-09 23:15
mveCPallini2-Nov-09 23:15 
QuestionMaking an Application Hang. Pin
Hari_162-Nov-09 22:52
Hari_162-Nov-09 22:52 
AnswerRe: Making an Application Hang. Pin
CPallini2-Nov-09 22:57
mveCPallini2-Nov-09 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.