2009-07-23 19 views
0
HANDLE Proc; 
HMODULE hDLL; 
hDLL = LoadLibrary(TEXT("mscoree.dll")); 
if(hDLL == NULL) 
    cout << "No Dll with Specified Name" << endl; 
else 
    { 

    cout << "DLL Handle" << hDLL << endl<<endl; 
    cout << "Getting the process address..." << endl; 
    Proc = GetProcAddress(hDLL,"GetRequestedRuntimeVersion"); 

    if(Proc == NULL) 
     { 
     FreeLibrary(hDLL); 
     cout << "Process load FAILED" << endl; 
     } 

    else 
     { 
     cout << "Process address found at: " << Proc << endl << endl; 
     LPWSTR st;DWORD* dwlength; ;DWORD cchBuffer=MAX_PATH; 
     HRESULT hr=GetCORSystemDirectory(st,cchBuffer,dwlength); 
     if(hr!=NULL) 
     { 
      printf("%s",hr); 
     } 
     FreeLibrary(hDLL); 
     } 
    } 

Ich mochte dies, um den .NET-Installationspfad zu bekommen, aber ich erhalte Linker-Fehler.Wie verwende ich GetCORSystemDirectory()?

Fehler LNK2019: nicht aufgelöstes externes Symbol _GetCORSystemDirectory @ 12 in Funktion verwiesen _main dot.obj

Antwort

1

definieren die GetCORSystemDirectory Signatur:

typedef HRESULT (__stdcall *FNPTR_GET_COR_SYS_DIR) (LPWSTR pbuffer, DWORD cchBuffer, DWORD* dwlength); 

die Funktionszeiger initialisieren:

FNPTR_GET_COR_SYS_DIR GetCORSystemDirectory = NULL; 

einen Funktionszeiger von mscoree.dll und Nutzung erhalten:

GetCORSystemDirectory = (FNPTR_GET_COR_SYS_DIR) GetProcAddress (hDLL, "GetCORSystemDirectory"); 
if(GetCORSystemDirectory!=NULL) 
{ 
    ... 
    //use GetCORSystemDirectory 
    ... 
} 

Wie gewünscht:

#ifndef _WIN32_WINNT    
#define _WIN32_WINNT 0x0600  
#endif 
#include <stdio.h> 
#include <tchar.h> 
#include <windows.h> 

typedef HRESULT (__stdcall *FNPTR_GET_COR_SYS_DIR) (LPWSTR pbuffer, DWORD cchBuffer, DWORD* dwlength); 
FNPTR_GET_COR_SYS_DIR GetCORSystemDirectory = NULL; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    HINSTANCE hDLL = LoadLibrary(TEXT("mscoree.dll")); 

    GetCORSystemDirectory = (FNPTR_GET_COR_SYS_DIR) GetProcAddress (hDLL, "GetCORSystemDirectory"); 
    if(GetCORSystemDirectory!=NULL) 
    { 
     TCHAR buffer[MAX_PATH]; 
     DWORD length; 
     HRESULT hr = GetCORSystemDirectory(buffer,MAX_PATH,&length); 

     // buffer should contain the folder name 
     // use it.. 

    } 

    return 0; 
} 
+0

Die if-Anweisung ist falsch. if (ptr! = 0) {// benutze} – sharptooth

+0

@sharptooth: Danke! korrigiert es. – Indy9000

+0

Lesen Sie die Dokumentation zu GetCORSystemDirectory, um zu verstehen, was es tut. Hier ist ein Link http://msdn.microsoft.com/en-us/library/k0588yw5(VS.71).aspx – Indy9000

0

Sie müssen "GetCORSystemDirectory" als zweiten Parameter in GetProcAddress() übergeben - es einen Zeiger auf diese Funktion Implementierung zurück. Dann werden Sie den Zeiger entsprechend darstellen und die Funktion durch sie aufrufen.