问题描述
好友。
我打算在MFC应用程序中调用用cygwin编译的dll。
我找不到正确的方法。
我尝试了什么:
例如,我的dll的头文件如下:
Hi, friends.
I'm going to call the dll compiled with cygwin at MFC application.
I could not find the correct way.
What I have tried:
For example, My dll's header file is following:
#ifndef _MYDLL_H_
#ifdef __cplusplus
extern "C" {
#endif
extern int(* fn_err_msg)(const char*, int type);
void init_engine();
void register_err_func(char(*lpErrorFunc)(const char* msg, char type));
#ifdef __cplusplus
}
#endif
#endif // _MYDLL_H_
我的dll的源文件如下:
My dll's source file is following:
...
void register_err_func(char(*lpErrorFunc)(const char* msg, char type))
{
fn_err_msg = lpErrorFunc;
out_debug("registered msg function.");
}
void out_debug (const char *message, ...)
{
if (fn_err_msg == NULL) {
va_list args;
fprintf(stderr, "MYDLL_DEBUG: ");
va_start(args, message);
vfprintf(stderr, message, args);
va_end(args);
fprintf(stderr, ".\n");
}
else {
char pszErrorMsg[1024] = "";
va_list args;
va_start(args, message);
vsprintf(pszErrorMsg, message, args);
va_end(args);
(fn_err_msg)(pszErrorMsg, eMsgDebug);
}
}
void init_engine()
{
out_debug("init_eninge");
pthread_t thread1;
// Init the session mutex
pthread_mutex_init(&session_mutex, NULL);
pthread_create(&thread1, NULL, handle_alarm_management, NULL);
// To handle exit signals
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
return 0;
}
...
makefile是......
gcc -O3 -fPIC -shared -ffunction-sections -I / usr / include -DHAVE_CONFIG_H -c mydll.c -o .. \ bin / mydll.o -MD -MF .. \ bin /mydll.dep
g ++ -shared -o .. \ bin / mydll.dll -Wl,-gc-sections .. \ bin / mydll.o
mfc应用程序代码如下:
makefile is ...
gcc -O3 -fPIC -shared -ffunction-sections -I/usr/include -DHAVE_CONFIG_H -c mydll.c -o ..\bin/mydll.o -MD -MF ..\bin/mydll.dep
g++ -shared -o ..\bin/mydll.dll -Wl,-gc-sections ..\bin/mydll.o
mfc application code is following:
////////////////////////////////////////////////
// Example.cpp
////////////////////////////////////////////////
...
HINSTANCE m_hCygwinInst;
typedef void(*pFnCygWinInit)();
extern pFnCygWinInit fn_cygwin_init;
BOOL CExample::InitInstance()
{
m_hCygwinInst = LoadLibrary("cygwin1.dll");
if (m_hCygwinInst == NULL) return FALSE;
fn_cygwin_init = (pFnCygWinInit)GetProcAddress(m_hCygwinInst, "cygwin_dll_init");
if (fn_cygwin_init == NULL) return FALSE;
...
}
////////////////////////////////////////////////
// ExampleDlg.cpp
////////////////////////////////////////////////
...
typedef void(*pFnInitEngine)();
typedef void(*pFnRegErrFunc)(char(*lpErrorFunc)(const char* msg, char type));
pFnInitEngine fn_init_engine = NULL;
pFnRegErrFunc fn_reg_err_func = NULL;
HINSTANCE m_hDllInst = NULL;
CExampleDlg* g_pMain = NULL;
...
char report_msg(const char* psz_msg, char type)
{
if (g_pMain)
g_pMain->add_msg(psz_msg, type);
return 0;
}
BOOL CExampleDlg::init_funcs()
{
BOOL bRes = FALSE;
g_pMain = this;
m_hDllInst = LoadLibrary("mydll.dll");
if (m_hDllInst == NULL) return bRes;
fn_init_engine = (pFnInitEngine)GetProcAddress(m_hDllInst, "init_engine");
fn_reg_err_func = (pFnRegErrFunc)GetProcAddress(m_hDllInst, "register_err_func");
if (!fn_init_engine || !fn_reg_err_func) {
return bRes;
}
bRes = TRUE;
return bRes;
}
BOOL CExampleDlg::OnInitDialog()
{
...
g_pMain = this;
if (init_funcs()) {
(fn_cygwin_init)(); // (1)
(fn_reg_err_func)(report_msg); // (2)
(fn_init_engine)(); // (3)
}
else {
AfxMessageBox(_T("Error!!!"));
CDialogEx::OnCancel();
return FALSE;
}
return TRUE;
}
void CExampleDlg::add_msg(const char* pszMsg, int type)
{
int n = m_lstMsgs.GetItemCount();
CString s;
s.Format(_T("%d"), n + 1);
m_lstMsgs.InsertItem(n, s);
if (pszMsg) {
CA2T pss(pszMsg);
m_lstMsgs.SetItemText(n, 1, pss.m_psz);
}
}
(1),(2):正确(3:评论)
(1),(3):正确(2:评论)
(1),(2),(3):没有回应。
但是,如果代码修改如下,那就没关系。
(1), (2): correctly (3: commented)
(1), (3): correctly (2: commented)
(1), (2), (3): has no response.
but, if the code is modified as follows, it is okay.
char report_msg(const char* psz_msg, char type)
{
// if (g_pMain)
// g_pMain->add_msg(psz_msg, type);
return 0;
}
这是什么原因?我该如何解决?
谢谢。
What's the reason? How can I fix it?
Thanks.
推荐答案
fn_init_engine()
函数,它将在
handle_alarm_management
启动的线程开始。写一些输出。
通常你应该在两个二进制文件都是调试版本时调试它。在我对cygwin的小知识中,我只知道当两个运行时都显示窗口时,MFC和cygwin都有问题。
startup. Write some output.
Normally you should debug it when both binaries are a debug build. In my "little knowing" about cygwin I only know that MFC and cygwin have problems when both runtimes are showing windows.
这篇关于MFC应用程序和cygwin DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!