Indy Client in dll



  • Hallo, ich habe ein dll mit XE8 geschrieben und darin Indy tcp client reingepackt. Dabei bekomme ich ein crash, komme da nicht so weiter.

    das ist main.cpp

    //   Important note about DLL memory management when your DLL uses the
    //   static version of the RunTime Library:
    //
    //   If your DLL exports any functions that pass String objects (or structs/
    //   classes containing nested Strings) as parameter or function results,
    //   you will need to add the library MEMMGR.LIB to both the DLL project and
    //   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
    //   if any other projects which use the DLL will be performing new or delete
    //   operations on any non-TObject-derived classes which are exported from the
    //   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
    //   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
    //   the file BORLNDMM.DLL should be deployed along with your DLL.
    //
    //   To avoid using BORLNDMM.DLL, pass string information using "char *" or
    //   ShortString parameters.
    //
    //   If your DLL uses the dynamic version of the RTL, you do not need to
    //   explicitly add MEMMGR.LIB as this will be done implicitly for you
    
    #include <windows.h>
    #include "ZLibrary.h"
    
    #pragma hdrstop
    #pragma argsused
    
    int WINAPI DllEntryPoint( HINSTANCE hinst, unsigned long reason, void* lpReserved ) {
    	switch ( reason ) {
    	case DLL_PROCESS_ATTACH: break;
    	case DLL_THREAD_ATTACH: break;
    	case DLL_THREAD_DETACH: break;
    	case DLL_PROCESS_DETACH: {
    		CleanSocket();
    	} break;
    	}
    
    	return TRUE;
    }
    

    das ist ZLibrary.h

    #ifndef ZLIBRARY_H
    #define ZLIBRARY_H
    
    #ifdef __cplusplus
    	#define ZMANGLING_NAME extern "C"
    #else
    	#define ZMANGLING_NAME
    #endif
    
    struct ZParameter {
    	unsigned short	Port;
    	char			IP[ 15 ];
    	char			IdToken[ MAX_PATH ];
    };
    
    #ifdef ZFEEDER_EXPORTS
    	#define ZFEEDER_API ZMANGLING_NAME __declspec( dllexport )
    #else
    	#define ZFEEDER_API ZMANGLING_NAME __declspec( dllimport )
    
    	typedef void __stdcall ( *pfnInitSocket )( ZParameter& parameter );
    	typedef void __stdcall ( *pfnCleanSocket )();
    #endif
    
    ZFEEDER_API void __stdcall InitSocket( ZParameter& parameter );
    ZFEEDER_API void __stdcall CleanSocket();
    
    #endif
    

    das ist ZLibrary.cpp

    #include <IdTCPClient.hpp>
    #include "ZLibrary.h"
    
    #pragma link "IndyCore.bpi"
    
    class ZConnectThread;
    class ZEvent;
    
    TIdTCPClient*		g_pClient			= NULL;
    ZConnectThread*		g_pConnectThread	= NULL;
    ZEvent*				g_pEvent			= NULL;
    ZParameter			g_Parameter			= { 0 };
    
    class ZConnectThread: public TThread {
    protected:
    	TIdTCPClient* m_pClient;
    
    	 void __fastcall Execute();
    
    public:
    	ZConnectThread( TIdTCPClient* pClient );
    };
    
    class ZEvent {
    public:
    	void __fastcall OnConnected( TObject *Sender );
    	void __fastcall OnDisconnected( TObject *Sender );
    };
    
    void __fastcall ZEvent::OnDisconnected( TObject *Sender ) {
    	if ( g_pConnectThread && g_pConnectThread->CheckTerminated() ) {
    		// g_pConnectThread->Start();
    	}
    }
    
    void __fastcall ZEvent::OnConnected( TObject *Sender ) {
    
    }
    
    ZConnectThread::ZConnectThread( TIdTCPClient* pClient ) {
    	this->m_pClient = pClient;
    }
    
    void __fastcall ZConnectThread::Execute() {
    	while ( !this->Terminated && !this->m_pClient->Connected() ) {
    		try {
    			this->m_pClient->Connect();
    		} catch( const EIdSocketError& ex ) {
    			Sleep( 1000 );
    		}
    	}
    }
    
    ZFEEDER_API void __stdcall InitSocket( ZParameter& parameter ) {
    	g_Parameter = parameter;
    
    	if ( !g_pClient )
    		g_pClient = new TIdTCPClient;
    
    	if ( g_pClient ) {
    		g_pClient->Host = parameter.IP;
    		g_pClient->Port = parameter.Port;
    
    		if ( !g_pEvent )
    			g_pEvent = new ZEvent;
    
    		if ( !g_pConnectThread ) {
    			g_pConnectThread = new ZConnectThread( g_pClient );
    			if ( g_pConnectThread )
    				g_pConnectThread->FreeOnTerminate = false;
    		}
    
    		if ( g_pEvent ) {
    			g_pClient->OnConnected 		= g_pEvent->OnConnected;
    			g_pClient->OnDisconnected	= g_pEvent->OnDisconnected;
    		}
    		/*
    		g_pClient->OnDisconnected 	= g_pEvent->OnDisconnected;
    		g_pClient->OnWork 			= g_pEvent->OnWork;
    		*/
    	}
    }
    
    ZFEEDER_API void __stdcall CleanSocket() {
    	if ( g_pConnectThread ) {
    		g_pConnectThread->Terminate();
    		g_pConnectThread->WaitFor();
    		delete g_pConnectThread;
    	}
    
    	if ( g_pClient ) {
    		if ( g_pClient->Connected() ) // hier kommt es zum crash
    			g_pClient->Disconnect();
    		delete g_pClient;
    	}
    }
    

Anmelden zum Antworten