views:

173

answers:

2
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);
  }
 }

i did like this to get the .NET installation path but i am getting Linker Errors.

error LNK2019: unresolved external symbol _GetCORSystemDirectory@12 referenced in function _main dot.obj

A: 

You need to pass "GetCORSystemDirectory" as the second parameter into GetProcAddress() - it will return a pointer to that function implementation. Then you'll cast the pointer appropriately and call the function through it.

sharptooth
+1  A: 

define the GetCORSystemDirectory signature:

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

initialise the function pointer:

FNPTR_GET_COR_SYS_DIR   GetCORSystemDirectory = NULL;

get a function pointer from mscoree.dll and use:

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

As requested:

#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;
}
Indeera
The if-statement is wrong. if( ptr != 0 ) { //use }
sharptooth
@sharptooth: Thanks! corrected it.
Indeera
Read the documentation on GetCORSystemDirectory to understand what it does. Here's a linkhttp://msdn.microsoft.com/en-us/library/k0588yw5(VS.71).aspx
Indeera
@Cute: Yeap, that's the path where .NET is installed.@Indeera: Size of buffer should be MAX_PATH + 1 for the sake of trailing zero paranoia and avoiding possible buffer overruns.
sharptooth
@sharptooth: No need, I'm passing in the size of the buffer.
Indeera
Sure, but the code that will deal with the buffer contents afterwards will need to take that length into account too. That's risky. It's much better to make a buffer larger by one and zero it out entirely - the string will become null-terminated automatically.
sharptooth
sharptooth: ofcourse in production code, but that's an entire evening of discussion :) This is just an illustration.
Indeera
How can i print the buffer value.i tried cout<<buffer but it doesnot work
Cute
if you are compiling in Unicode mode use wcout << buffer << endl;
Indeera
with cout i cant display it na???
Cute
how can i convert it to string or char[]
Cute
Use WideCharToMultiByte() to convert from Unicode to ANSI
sharptooth