Thread: How to load another process

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    50

    How to load another process

    Look:
    Code:
    (1)
    extern "C" _declspec(dllexport) int add(int a,int b)
    {
    	return a+b;
    }
    
    int main()
    {
    	printf("a+b=%d",add(2,6));
    	getchar();
    	return 0;
    }
    (2)
    int main()
    {
    	typedef int (*FUN)(int,int);
    	FUN fun;
                HMODULE hmod=LoadLibrary("dllexe.exe");
                if(NULL==hmod)
    	{
    		printf("error load\n");
    		return -1;
    	}
                fun=(FUN)GetProcAddress(hmod,"add");
                if(NULL==fun)
    	{
    		printf("error GetProcAddress\n");
    		return -1;
    	}
    	
                int sum=(*fun)(18,22);
    	printf("sum=%d\n",sum);
    	FreeLibrary(hmod);
    	getchar();
    	return 0;
    }
    The first process export a function,and when it is running,I want to know
    how the second process load the first process?(Because they are both independent processes,and they have independent 4G virtual memory)

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    (1), when compiled and linked, is a module. It doesn't become a process until it is executed.
    (2) loads module 1, not process 1.
    If you execute both 1 and 2 at the same time then yes, they are independent processes. But the module 1 which is part of process 2 is not a process in it self, and they share the same address space.
    There is absolutely no inter-process communication going on here.
    Last edited by _Mike; 08-15-2011 at 07:37 AM. Reason: reworded for clarity

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want program 2 to start program 1... use CreateProcess() ... CreateProcess Function (Windows)

    If you are trying to write a DLL... Dynamic-Link Libraries (Windows)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get process info ( to extract process thread id )
    By umen242 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2009, 01:08 PM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. load user-mode dll into kernel-mode "process"
    By Yarin in forum Windows Programming
    Replies: 10
    Last Post: 12-12-2008, 04:56 PM
  4. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM