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_
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;
pthread_mutex_init(&session_mutex, NULL);
pthread_create(&thread1, NULL, handle_alarm_management, NULL);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
return 0;
}
...
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:
...
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;
...
}
...
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)(); (fn_reg_err_func)(report_msg); (fn_init_engine)(); }
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): 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)
{
return 0;
}
What's the reason? How can I fix it?
Thanks.