C++在dll中獲取自身路徑(非exe調用路徑)

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std;

HMODULE GetSelfModuleHandle()
{
    MEMORY_BASIC_INFORMATION mbi;
    return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL);
}

extern "C" __declspec(dllexport) int GetDllPath(){
	ifstream file;
	char curDir[100] = {0};
	GetModuleFileName(GetSelfModuleHandle(),curDir,100);
	cout<<"Dll Path:"<<curDir<<endl;
	return 0;
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	cout<<"DllMain called"<<endl;
	return TRUE;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章