dll through QTMessaging, Developer swears its C/CPP

Just starting out? Need help? Post your questions and find answers here.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

dll through QTMessaging, Developer swears its C/CPP

Post by LiK137 »

Hi,
I have been reposting same problem years as there are almost thousands of views but zero replies.
I am far from Class, Methods and OOP way that is why I noway understand the algorithm of source.

from header file:

Command.h:

Code: Select all

#define INITDONE_EVENT		4
#define STATECHANGED_EVENT	5


typedef enum L_ERROR
{
	L_NO_ERROR = 0, /*!< No errors */
	L_CONNECT_ERROR = 1, /*!< The Error of connection */
	L_MEMORY_ERROR = 2, /*!< Out of memory */
	L_RESOURCES_ERROR = 3, /*!< The Error of the allocating resource IPC */
	L_EXIT_ERROR = 7, /*!< The thread terminated */
	L_ARG_ERROR = 10, /*!< Wrong function arguments. */
} L_ERROR;

typedef enum L_COMMAND
{
	L_CMD_NOP = 0, /*!< empty command */
	L_CMD_EXIT = 1, /*!< terminate the thread */
	L_CMD_STOP = 2, /*!< stop the mode of waiting for command */
	L_CMD_START = 6, /*!< start the mode of waiting for command  */

} L_COMMAND;


typedef struct L_HANDLE
{
  L_COMMAND command;
  void* visual_ptr;
  QMutex* image_lock;
}L_HANDLE;

/*Get command to Client thread*/
L_COMMAND getCommand(L_HNDL handle)

/*Send command to the client library thread.*/
void sendCommand(L_HNDL handle, L_COMMAND command );


typedef struct LClient
{
		L_HNDL Client;
		L_COMMAND (*GetCommand)( L_HNDL Cl );
} LSClient;

__declspec(dllexport) L_ERROR LInit( LClient *cl, L_DEV dev, int num );

__declspec(dllexport) L_ERROR LStart( LClient *cl );

__declspec(dllexport) void LShutDown( LClient* cl );


Command.cpp:

Code: Select all

L_COMMAND getCommand( L_HNDL handle )
{
	L_HANDLE* hndl = (L_HANDLE*) handle;
	L_COMMAND cmd;

	cmd = hndl->command;
	hndl->command = L_CMD_NOP;

	return cmd;
}

void sendCommand( L_HNDL handle, L_COMMAND command )
{
	L_HANDLE* hndl = (L_HANDLE*) handle;

	while( hndl->command != L_CMD_NOP )
	{
#ifdef _WIN32
		SleepEx( 10, TRUE );
#else
		usleep( 10000 );
#endif
	}

	hndl->command = command;
}

void initDone( L_HNDL handle )
{
	L_HANDLE* hndl = (L_HANDLE*) handle;
	QCustomEvent* event = new QCustomEvent( (QEvent::Type) (QEvent::User + INITDONE_EVENT) );

	QApplication::postEvent( (QObject*) hndl->visual_ptr, event );
}

MainForm.cpp:

Code: Select all

/* Send start command to lclient */
void mainForm::startSlot()
{
	sendCommand( &l_handle, L_CMD_START );
}

/* Send stop  command to lclient */
void mainForm::stopSlot()
{
	sendCommand( &l_handle, L_CMD_STOP );
}


This is unusual way for me and I can not understand to translate to PB and no hint in the forum.

Will be very glad for any explanation
User avatar
skywalk
Addict
Addict
Posts: 3995
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: dll through QTMessaging, Developer swears its C/CPP

Post by skywalk »

What is the question?
I see simple C struct's and enumerations and prototypes defined.
I do not see?

Code: Select all

#if defined(__cplusplus) || defined(__cplusplus__)
extern "C" {
#endif
...
...
#if defined(__cplusplus) || defined(__cplusplus__)
}
#endif
So, not sure it is compiled as straight C dll? Use dependency walker to open the dll and confirm the functions are not mangled.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: dll through QTMessaging, Developer swears its C/CPP

Post by LiK137 »

Here You Are IfDefs

Code: Select all

#if defined(__WIN32__) || defined(WIN32)

#ifdef LCLIENT_EXPORTS
#define LCLIENT_API __declspec(dllexport)
#else
#define LCLIENT_API __declspec(dllimport)
#endif
#include <windows.h>
#else
#define LCLIENT_API
#endif

#ifdef __cplusplus
extern "C"
{
#endif

#if defined(_MSC_VER)
#pragma warning( disable : 4103 )
#endif

#if defined(__BORLANDC__)
#pragma warning(push)
#if (__BORLANDC__  > 0x0520)
#pragma warn -8059
#else
#pragma warn -pck
#endif
#endif

#if defined (__IBMCPP__)
#pragma pack(1)
#endif

#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__GNUC__)
#pragma pack(push,1)
#define PACKED_1
#endif

and Footer Parts:

Code: Select all

#if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__IBMCPP__) || defined(__GNUC__)
#pragma pack(pop)
#endif

#ifdef __cplusplus
}
#endif

#endif

LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: dll through QTMessaging, Developer swears its C/CPP

Post by LiK137 »

This "command.cpp" and "MainForm.cpp" cannot be understood.

If it helps I'd add some more:

Code: Select all

//---------------------------------------------------------------------------
L_COMMAND L_GetCommand(L_HNDL Cl)
{
	libConnect *Client = static_cast<libConnect *>( Cl );
	return Client->GetCommand();
}


//---------------------------------------------------------------------------
void libConnect::InitDone()
{
	PostMessage( client_wnd_, WM_INITED, 0, 0 );
}

//---------------------------------------------------------------------------
void libConnect::SetCommand(L_COMMAND cmd)
{
	EnterCriticalSection( &cs );
	curr_command_ = cmd;
	LeaveCriticalSection( &cs );
}

//---------------------------------------------------------------------------
L_COMMAND libConnect::GetCommand()
{
	L_COMMAND res = L_CMD_NOP;
	EnterCriticalSection( &cs );
	res = curr_command_;
	curr_command_ = L_CMD_NOP;
	LeaveCriticalSection( &cs );
	return res;
}


//---------------------------------------------------------------------------
LRErr libConnect::start()
{
	SetCommand( L_CMD_START );
	return LR_NONE;
}

//---------------------------------------------------------------------------
LRErr Start(H_DEV h)
{
	if( h )
	{
		libConnect* r_manager = static_cast<libConnect*>( h );
		return r_manager->start();
	}
	return LR_BAD_HANDLE;
}
//---------------------------------------------------------------------------
LRErr Stop(H_DEV h)
{
	if( h )
	{
		libConnect* r_manager = static_cast<libConnect*>( h );
		return r_manager->stop();
	}
	return LR_BAD_HANDLE;
}

As seen "static_cast" , "CriticalSection", "PostMessage" and from cpp files class calls possible to use in PureBasic?
Post Reply