Thread: loading a dll
View Single Post
Re: loading a dll
Old
  (#13)
dub
Member
 
dub's Avatar
 
Status: Offline
Posts: 89
Join Date: Aug 2004
Location: UK
Default Re: loading a dll - 22-12-2004

here a way windows only using CreateToolhelpSnapshot Then Module32First
& Module32Next.

Code:
class CProcess  
{
public:
	CProcess () : dwProcessID(NULL), dwThreadId(NULL), iStrLen(0) { }
	virtual	~CProcess () { }
	DWORD	GetProcessIDFromExeName (LPSTR lpExecutableName);
	MODULEENTRY32 GetModuleEntryForDll (LPSTR lpExeName, LPSTR lpDllName);
	
	HANDLE		hSnapshot;
	PROCESSENTRY32  pe32;
	MODULEENTRY32	me32;
private:
	DWORD		dwProcessID;
	DWORD		dwThreadId;
	int			iStrLen;
	char		        szExeName[64];
};

DWORD CProcess::GetProcessIDFromExeName (LPSTR lpExecutableName)
{
	if ((hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPALL, NULL)) == INVALID_HANDLE_VALUE)
		return NULL;

	pe32.dwSize = sizeof (pe32);

	if (!Process32First(hSnapshot, &pe32))
	{
		CloseHandle (hSnapshot);
		return NULL;
	}

	// scan all the open process`s on the computer
	do
	{
		iStrLen = strlen (pe32.szExeFile);

		strcpy (szExeName, &pe32.szExeFile[iStrLen]);
		
		// skip the backward slash and get just the exe name
		// if it returns the full path name doesn`t seem to do
		// it in XP just the exe name.
		if (strstr (pe32.szExeFile, "/") != NULL)
		{
			while ((iStrLen) && (pe32.szExeFile[iStrLen-1] != '/'))
				iStrLen--;
			strcpy (szExeName, &pe32.szExeFile[iStrLen]);
		}

		// some reason break didn`t seem to work
		if (strcmpi (lpExecutableName, szExeName) == 0)
			goto done;
		
	} while (Process32Next (hSnapshot, &pe32) != 0);

done:
	CloseHandle (hSnapshot);
	return (pe32.th32ProcessID);
}

MODULEENTRY32 CProcess::GetModuleEntryForDll (LPSTR lpExeName, LPSTR lpDllName)
{
	if ((hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, GetProcessIDFromExeName (lpExeName))) == INVALID_HANDLE_VALUE)
	{
		ZeroMemory (&me32, sizeof (me32));
		return me32;
	}

	me32.dwSize = sizeof (me32);

	if (!Module32First (hSnapshot, &me32))
	{
		CloseHandle (hSnapshot);
		ZeroMemory (&me32, sizeof (me32));
		return me32;
	}

	// scan all the open process`s on the computer
	do
	{
		// some reason break didn`t seem to work
		if (strcmpi (lpDllName, me32.szModule) == 0)
			goto done;
		
	} while (Module32Next (hSnapshot, &me32) != 0);

done:
	CloseHandle (hSnapshot);
	return (me32);
}
after you will have full access for the dll.

Last edited by dub; 22-12-2004 at 21:29..
  
Reply With Quote