在visual studio中使用cygwin编译的dll的库函数时出错

时间:2016-06-13 12:00:52

标签: c++ c visual-studio cygwin

我试图在工作室环境中调用cygwin编译的dll。

如果我编译没有任何库的函数的dll(只返回任何数字), 它工作正常,但如果我调用例如stdio.h,并且使用写入文件或只是printf函数,则不起作用(如果printf函数已退出代码1536)。

#include <stdio.h>

 int myfunc() 
{
  char* strtxt = "test";
  FILE *hF = fopen( "Newlogtst.txt", "w" );   
  if(hF == 0)
  {
    return 5;
  }    
  fputs( (const char*)strtxt, hF );
  fclose(hF);
  return 1;
}

int tst()
{
     return 25;
}

函数tst工作正常,函数myfunc make empty file Newlogtst.txt并显示异常。 `

  

在CygwinDlltest.exe中的0x6113333A(cygwin1.dll)抛出异常:   0xC0000005:访问冲突读取位置0x004E0059。

     

如果存在此异常的处理程序,则程序可能是安全的   继续进行。

` 在visual studio中我正在使用此代码

#include <windows.h>

typedef int (*PFN_HELLO)();
typedef void (*PFN_CYGWIN_DLL_INIT)();

int main()
{
    PFN_HELLO func;
    HMODULE hLib, h = LoadLibrary(TEXT("C:\\cygwin\\bin\\cygwin1.dll")); 
    PFN_CYGWIN_DLL_INIT init = (PFN_CYGWIN_DLL_INIT) GetProcAddress(h,"cygwin_dll_init");
    init(); 

    hLib = LoadLibrary (TEXT("C:\\Cygwin\\home\\azatyan\\TestDynamicLink\\mydll.dll"));
    func = (PFN_HELLO) GetProcAddress (hLib, "myfunc");
    return func();      
}

请帮助我做什么来使用库函数。

1 个答案:

答案 0 :(得分:1)

  1. 您不会检查GetProcAddress()
  2. 的返回代码
  3. 如果你用C ++编译它,那么名称就会受到不同的影响(这就是为什么GetprocAddress()会返回NULL btw。)因为它们是不同的编译器。
  4. 如果您只是使用示例中的基本功能,则应将它们声明为extern "C",以免它们受到损坏。还要确保在编译DLL时正确使用__declspec export语句。