Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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:

C++
...
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 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:

C++
////////////////////////////////////////////////
// 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): 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.
Posted
Updated 20-Nov-17 2:23am
v3
Comments
Richard MacCutchan 11-Sep-17 12:08pm    
The reason for what? Your question is unclear.
forest-321 11-Sep-17 12:26pm    
first, reason for no response.
second, how to call the dll compiled with cygwin at MFC application.
forest-321 11-Sep-17 12:40pm    
what does cygwin dll init work?

1 solution

As I understand your code the problem is in the
fn_init_engine()
function and it will be in thread start of
handle_alarm_management
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.
 
Share this answer
 
Comments
forest-321 20-Nov-17 21:59pm    
Thank you for your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900