Click here to Skip to main content
15,888,286 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow make a dll in VC++ to use in C#? Pin
Rui A Sousa28-Aug-07 16:29
Rui A Sousa28-Aug-07 16:29 
AnswerRe: How make a dll in VC++ to use in C#? Pin
Karismatic28-Aug-07 20:25
Karismatic28-Aug-07 20:25 
GeneralRe: How make a dll in VC++ to use in C#? Pin
Rui A Sousa29-Aug-07 3:33
Rui A Sousa29-Aug-07 3:33 
GeneralRe: How make a dll in VC++ to use in C#? Pin
Hamid_RT29-Aug-07 7:51
Hamid_RT29-Aug-07 7:51 
QuestionOpenFileMapping call Fails on VISTA Pin
praveenpl28-Aug-07 13:53
praveenpl28-Aug-07 13:53 
QuestionWhy ain't this in the docs? Pin
Waldermort28-Aug-07 13:24
Waldermort28-Aug-07 13:24 
AnswerRe: Why ain't this in the docs? Pin
Naveen28-Aug-07 15:55
Naveen28-Aug-07 15:55 
GeneralRe: Why ain't this in the docs? Pin
Waldermort28-Aug-07 16:02
Waldermort28-Aug-07 16:02 
Well, it's a bit long winded, but something like this:


	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	LPTSTR szExeName = GetCommandLine();

	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );

	// Start the child process. 
	if( ! ::CreateProcess( L"Demo.exe", 0, 0, 0, 0, CREATE_SUSPENDED, 0, 0, &si, &pi ) ) 
		return NULL;

	HANDLE hProcess = OpenProcess(  PROCESS_ALL_ACCESS , FALSE, pi.dwProcessId );

	CONTEXT context;
	ZeroMemory( &context, sizeof( CONTEXT ) );
	context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
	if ( ! GetThreadContext( pi.hThread, &context ) )
		return 0;

	LDT_ENTRY entry;
	ZeroMemory( &entry, sizeof( LDT_ENTRY ) );
	GetThreadSelectorEntry( pi.hThread, context.SegFs, &entry );
	DWORD dwFSBase = ( entry.HighWord.Bits.BaseHi << 24 ) | ( entry.HighWord.Bits.BaseMid << 16 ) | ( entry.BaseLow );

	DWORD read;
	Local::TEB teb; ZeroMemory( &teb, sizeof( Local::TEB ) );
	Local::PPEB pPeb = NULL;
	if ( ReadProcessMemory( hProcess, (LPCVOID)(DWORD_PTR)dwFSBase, &teb, sizeof( Local::TEB ), &read ) )
		pPeb = teb.Peb;
	else
		return NULL;

	IMAGE_DOS_HEADER DosHeader;
	IMAGE_NT_HEADERS NtHeader;

	DWORD dwBase = (DWORD)(DWORD_PTR)pPeb->ImageBaseAddress;
	DWORD dwSize = 0;

	ReadProcessMemory( hProcess, (LPCVOID)(DWORD_PTR)dwBase, &DosHeader, sizeof( IMAGE_DOS_HEADER ), &read );
	if ( DosHeader.e_magic != IMAGE_DOS_SIGNATURE )
		throw "Not a valid executable file.\n";

	ReadProcessMemory( hProcess, (LPCVOID)(DWORD_PTR)(dwBase + DosHeader.e_lfanew), &NtHeader, sizeof( IMAGE_NT_HEADERS ), &read );
	if ( NtHeader.Signature != IMAGE_NT_SIGNATURE )
		throw "No PE header found.\n";

	dwSize = NtHeader.OptionalHeader.SizeOfImage;

	BOOL bSuccess = FALSE;
	DWORD dwOldProtect;

	bSuccess = VirtualProtectEx( hProcess, (PVOID)dwBase, m_dwImageSize, PAGE_EXECUTE_READWRITE, &dwOldProtect );

...

	bSuccess = VirtualProtectEx( hProcess, (PVOID)dwBase, m_dwImageSize, PAGE_EXECUTE, &dwOldProtect );

	CloseHandle( hProcess );

	ResumeThread( pi.hThread );

	return pi.hProcess;

I realize CreateProcess() should return with ALL_ACCESS_RIGHTS but I thought I would try the OpenProcess() also.
GeneralRe: Why ain't this in the docs? Pin
Naveen28-Aug-07 16:44
Naveen28-Aug-07 16:44 
GeneralRe: Why ain't this in the docs? Pin
Waldermort28-Aug-07 17:45
Waldermort28-Aug-07 17:45 
GeneralRe: Why ain't this in the docs? Pin
Naveen28-Aug-07 18:40
Naveen28-Aug-07 18:40 
GeneralRe: Why ain't this in the docs? Pin
Waldermort29-Aug-07 2:46
Waldermort29-Aug-07 2:46 
GeneralRe: Why ain't this in the docs? Pin
Naveen28-Aug-07 19:27
Naveen28-Aug-07 19:27 
GeneralRe: Why ain't this in the docs? Pin
Waldermort29-Aug-07 5:05
Waldermort29-Aug-07 5:05 
GeneralRe: Why ain't this in the docs? Pin
Waldermort28-Aug-07 16:08
Waldermort28-Aug-07 16:08 
GeneralRe: Why ain't this in the docs? Pin
Naveen28-Aug-07 16:46
Naveen28-Aug-07 16:46 
QuestionMFC Win32 Dialog application not being updated under Win-XP OS. Pin
Ralph_L28-Aug-07 12:24
Ralph_L28-Aug-07 12:24 
AnswerRe: MFC Win32 Dialog application not being updated under Win-XP OS. Pin
Mark Salsbery28-Aug-07 13:04
Mark Salsbery28-Aug-07 13:04 
QuestionRe: MFC Win32 Dialog application not being updated under Win-XP OS. Pin
David Crow29-Aug-07 3:23
David Crow29-Aug-07 3:23 
AnswerRe: MFC Win32 Dialog application not being updated under Win-XP OS. Pin
Mark Salsbery29-Aug-07 6:54
Mark Salsbery29-Aug-07 6:54 
GeneralRe: MFC Win32 Dialog application not being updated under Win-XP OS. Pin
David Crow29-Aug-07 7:09
David Crow29-Aug-07 7:09 
QuestionUsing the debugger Pin
Waldermort28-Aug-07 10:36
Waldermort28-Aug-07 10:36 
AnswerRe: Using the debugger Pin
Chris Losinger28-Aug-07 10:58
professionalChris Losinger28-Aug-07 10:58 
GeneralRe: Using the debugger Pin
Waldermort28-Aug-07 11:11
Waldermort28-Aug-07 11:11 
GeneralRe: Using the debugger Pin
Chris Losinger28-Aug-07 11:13
professionalChris Losinger28-Aug-07 11:13 

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.