0

I'm working on a project that need to use lib magic library to detect file mime type, I'm using 64 Bit version for windows (see: https://github.com/nscaife/file-windows) my project itself is C dll which I will call it from python. Loading the library is working fine, however when I use GetProcAddress() to access some function, it return NULL and the GetLastError() function return 126. See my code:

int DLL_EXPORT mag()
{
   char *actual_file = "test.db";
   const char *magic_full;
   HMODULE hModule = LoadLibrary("libmagic-1.dll");
   if(hModule == NULL) //No problem here
    return GetLastError();
   magic_t (*t0)(int) = (void *) GetProcAddress(hModule, "magic_open");
   const char (*t)(magic_t, const char *)  = (void *) 
   GetProcAddress(hModule, "magic_file");
   if(t0 == NULL && t == NULL);
    return GetLastError();
   magic_t magic_cookie;
   magic_cookie = t0(MAGIC_MIME);
   magic_full = t(magic_cookie, actual_file);
   return 0;
}

What is the problem Here?

Ahmad Issa
  • 313
  • 1
  • 2
  • 15
  • That's implausible. GetProcAddress won't lead to ERROR_MOD_NOT_FOUND. – David Heffernan Jul 24 '17 at 19:13
  • @DavidHeffernan Why? LoadLibrary is not making error and it doesn't return NULL, while GetProcAddress is returning NULL, not only this if I changed "magic_file" to other non sense name GetLastError will return 127 instead of 126. – Ahmad Issa Jul 24 '17 at 19:25
  • GetProcAddress returning module not found? Why you asking for the address of an export yield that error. – David Heffernan Jul 24 '17 at 19:26
  • really `GetProcAddress` return `ERROR_MOD_NOT_FOUND` in case `hModule != 0` but `hModule` not point to valid pe image – RbMm Jul 24 '17 at 19:27
  • So what you mean is that LoadLibrary is the source of the problem? – Ahmad Issa Jul 24 '17 at 19:31
  • @ParcRoi - i can not say exactly. need debug. or how minimum if you have symbol installed for ntdll.dll - set `LdrpDebugFlags` or `ShowSnaps` (this is alias) in ntdll to `0xFFFFFFBF` before call `GetProcAddress` and view output – RbMm Jul 24 '17 at 19:35
  • Depending on what you are really trying to accomplish, maybe [this](https://stackoverflow.com/a/20647203/1889329) is a solution. – IInspectable Jul 25 '17 at 13:44
  • @IInspectable Thanks a lot, your solution looks good and I'll consider it, However this solution looks like windows specific while libmagic is more portable to other systems (I want my code to be portable whenver it possible), And it may be more advanced solution. I guess I will go with your solution if this problem still unsolved. – Ahmad Issa Jul 25 '17 at 14:33

0 Answers0