Dynamischer Zugriff auf eine dll



  • Alexander Kempf schrieb:

    Bist Du Dir eigentlich sicher, daß die Signatur

    typedef int (__declspec(dllimport) TDPResult)(char*, char*);
    

    korrekt ist? Hast Du für die Dll eine Header-Datei oder sonst irgendeine
    Beschreibung?

    Gruß,

    Alexander

    ja einen header hab' ich und dort ist die Funktion so deklariert:

    EXPO1 DP_RESULT EXPO2 DPInit(LPSTR pDllName, LPSTR pInitSting);
    

    aber was LPSTR ist, hab' ich nirgends gefunden... 😞 und ich hab ein Delphi-Beispielk das funktioniert und dort werden PChar-Variablen gesendet und dann dachte ich, char* sei schon das richtige...
    Delaration in Delphi:

    function DPInit(pDllName, pInitSting : pchar) : TDPResult; stdcall;
     external 'PROF_DP.DLL';
    


  • Hallo,

    Aus der MSDN:

    LPSTR A 32-bit pointer to a character string



  • Braunstein schrieb:

    Hallo,

    Aus der MSDN:

    LPSTR A 32-bit pointer to a character string

    Oh, Vielen Dank, wusste ich nicht, die Borlandhilfe sagt nichts über LPSTR.
    Jetzt hab' ich alles (Deklarationen) auf LPSTR umgebogen, die Exception kommt aber immernoch 😞



  • roN schrieb:

    Delaration in Delphi:

    function DPInit(pDllName, pInitSting : pchar) : TDPResult; stdcall;
     external 'PROF_DP.DLL';
    

    Hast Du auch die Deklaration von TDPResult? Es muß nämlich schon alles
    stimmen.

    Gruß,

    Alexander



  • Alexander Kempf schrieb:

    Hast Du auch die Deklaration von TDPResult?

    ist das die Deklaration in Delphi? 😕

    type TDPResult =
    (
    	DP_NO_ERROR,                            // no error
    	DP_GENERIC_ERROR,			// generic error
    	DP_CALLED_TWICE,			// DP function have been called double (only with DPSetDispatchMessage())
    	DP_OPEN_FAILED,				// device (FDL) opening failed
    	DP_FDL_LOAD_FAILED,			// unable to load FDL library
    	DP_NOT_OPEN,				// FDL not opened
    	DP_THREAD_ENABLED,			// error, background thread enabled: function disabled
    	DP_NO_THREAD_ENABLED,		        // error, background thread is not enabled: function disabled
    	DP_OUT_OF_MEMORY,			// out of memory
    	DP_CONVERTER_FAILED,		        // PROFIBUS converter not found, failed or bad type
    	DP_ILL_ADDRESS,				// illegal DP slave address
    	DP_SAP_OPEN_FAILED,			// PROFIBUS SAP opening failed
    	DP_SLAVE_OPEN,				// slave is allread open
    	DP_SLAVE_NOT_OPEN,			// slave is not opened
    	DP_SLAVE_NOT_FOUND,			// slave not found
    	DP_ILLEGAL_CONFIG,			// illegal PROFIBUS configuration
    	DP_CFG_ERROR,				// sconfiguration error on startup
    	DP_PRM_ERROR,				// parameter error on startup
    	DP_CFG_AND_PRM_ERROR,		        // configuration and parameter error on startup
    	DP_BAD_LENGTH,				// data length incorrect
    	DP_INCOMPLETE,				// background operation initiated: use DPGetAsyncResult() for answer. High word holds operation ID */
    	DP_NORESULT				// no background operation finished DPGetAsyncResult()
    );
    


  • roN schrieb:

    ist das die Deklaration in Delphi? 😕

    Sieht für mich nicht so aus. Vielleicht solltest Du das auch mal
    in Deinen Code einbinden und als Rückgabetyp für Deine Funktion
    verwenden.

    Gruß,

    Alexander



  • Alexander Kempf schrieb:

    Vielleicht solltest Du das auch mal
    in Deinen Code einbinden und als Rückgabetyp für Deine Funktion
    verwenden.

    Und wie bieg' ich das auf C++ um?



  • roN schrieb:

    Und wie bieg' ich das auf C++ um?

    Per Copy&Paste



  • Alexander Kempf schrieb:

    Per Copy&Paste

    Jetzt hab' ich das so in den header unter public eingefügt, und der Compiler meint,"Typname erwartet"
    also mit copy &paste funktioniert das nicht einfach (oder ich hab's am falschen Ort eingefügt...)

    type TDPResult =
    (
        DP_NO_ERROR,                            // no error
        DP_GENERIC_ERROR,            // generic error
        DP_CALLED_TWICE,            // DP function have been called double (only with DPSetDispatchMessage())
        DP_OPEN_FAILED,                // device (FDL) opening failed
        DP_FDL_LOAD_FAILED,            // unable to load FDL library
        DP_NOT_OPEN,                // FDL not opened
        DP_THREAD_ENABLED,            // error, background thread enabled: function disabled
        DP_NO_THREAD_ENABLED,                // error, background thread is not enabled: function disabled
        DP_OUT_OF_MEMORY,            // out of memory
        DP_CONVERTER_FAILED,                // PROFIBUS converter not found, failed or bad type
        DP_ILL_ADDRESS,                // illegal DP slave address
        DP_SAP_OPEN_FAILED,            // PROFIBUS SAP opening failed
        DP_SLAVE_OPEN,                // slave is allread open
        DP_SLAVE_NOT_OPEN,            // slave is not opened
        DP_SLAVE_NOT_FOUND,            // slave not found
        DP_ILLEGAL_CONFIG,            // illegal PROFIBUS configuration
        DP_CFG_ERROR,                // sconfiguration error on startup
        DP_PRM_ERROR,                // parameter error on startup
        DP_CFG_AND_PRM_ERROR,                // configuration and parameter error on startup
        DP_BAD_LENGTH,                // data length incorrect
        DP_INCOMPLETE,                // background operation initiated: use DPGetAsyncResult() for answer. High word holds operation ID */
        DP_NORESULT                // no background operation finished DPGetAsyncResult()
    );
    


  • Hast Du Recht. Probier's mal mit enum anstatt type
    und definiere mal zusätzlich noch einen typ, z.B. so:

    enum EnTDPResult =
    (
        //...
    );
    typedef EnTDPResult TDPResult;
    

    Bin mir aber auch nicht 100% sicher und hab's auch nicht
    ausprobiert.

    Gruß,

    Alexander



  • Wenn ich das so mache:

    enum EnTDPResult =
    ( 
        DP_NO_ERROR,                            // no error
        DP_GENERIC_ERROR,            // generic error
        DP_CALLED_TWICE,            // DP function have been called double (only with DPSetDispatchMessage()) 
        DP_OPEN_FAILED,                // device (FDL) opening failed
        DP_FDL_LOAD_FAILED,            // unable to load FDL library 
        DP_NOT_OPEN,                // FDL not opened 
        DP_THREAD_ENABLED,            // error, background thread enabled: function disabled 
        DP_NO_THREAD_ENABLED,                // error, background thread is not enabled: function disabled 
        DP_OUT_OF_MEMORY,            // out of memory 
        DP_CONVERTER_FAILED,                // PROFIBUS converter not found, failed or bad type 
        DP_ILL_ADDRESS,                // illegal DP slave address 
        DP_SAP_OPEN_FAILED,            // PROFIBUS SAP opening failed 
        DP_SLAVE_OPEN,                // slave is allread open 
        DP_SLAVE_NOT_OPEN,            // slave is not opened 
        DP_SLAVE_NOT_FOUND,            // slave not found
        DP_ILLEGAL_CONFIG,            // illegal PROFIBUS configuration 
        DP_CFG_ERROR,                // sconfiguration error on startup 
        DP_PRM_ERROR,                // parameter error on startup 
        DP_CFG_AND_PRM_ERROR,                // configuration and parameter error on startup 
        DP_BAD_LENGTH,                // data length incorrect 
        DP_INCOMPLETE,                // background operation initiated: use DPGetAsyncResult() for answer. High word holds operation ID */ 
        DP_NORESULT                // no background operation finished DPGetAsyncResult() 
    );
           typedef EnTDPResult TDPResult;
    

    sagt der Compiler
    [C++ Fehler] TProfibus.h(13): E2447 'EnTDPResult' muß ein bereits definiertes enum sein.

    [edit] sorry, hab' EnTDPResult geschrieben, eigentlich...[/edit]



  • Das typedef brauchst du doch gar nicht. Mit dem enum ist doch schon alles deklariert. Mal abgesehen davon, hab ich mal irgendwo gelesen, dass der Rückgabetyp einer Funktion nicht zur Signatur gehört.



  • roN schrieb:

    Wenn ich das so mache:

    enum TDPResult =
    ( 
        // ...
    );
           typedef EnTDPResult TDPResult;
    

    sagt der Compiler
    [C++ Fehler] TProfibus.h(13): E2447 'EnTDPResult' muß ein bereits definiertes enum sein.

    Dann sieh Dir noch mal genau an, was ich geschrieben habe.
    Das ist doch ein kommerzielle Dll, oder? Gibt's denn da von Seiten des Herstellers keine
    ordentliche Unterstützung, Beispiele etc.?

    Gruß,

    Alexander



  • Alexander Kempf schrieb:

    Dann sieh Dir noch mal genau an, was ich geschrieben habe.

    Siehe [edit]

    Alexander Kempf schrieb:

    Das ist doch ein kommerzielle Dll, oder? Gibt's denn da von Seiten des Herstellers keine
    ordentliche Unterstützung, Beispiele etc.?

    ja, ist schon eine kommerzielle Dll und ich hab' auch ein beispiel, aber das kompiliert nicht mit dem BCB5 und ist mit statischer dll-Einbindung gemacht (hab' ich auch schon probiert, dann hat's von der Herstellerseite geheissen, das funktioniere nur mit BCB6...beim 5er hab' ich immer einen Linker-Error bekommen :() ein Beispiel das ich habe, ist für Delphi, das kompiliert ohne Probleme, dann hab' ich versucht den Delphicode mit dem BCB zu kompilieren=Wieder dieser Linker-Error ärger dann hab' ich gedacht, ich probiere mal, die Funktionen dynamisch zu laden...)



  • Hast Du's, wie Braunstein vorgeschlagen hat, auch mal ohne den typedef probiert?



  • Alexander Kempf schrieb:

    Hast Du's, wie Braunstein vorgeschlagen hat, auch mal ohne den typedef probiert?

    ja, hab' ich= selbes Ergebniss... 😞



  • Versuchen wir es doch mal anders.
    Was war das denn für ein Linker-Error?



  • Braunstein schrieb:

    Was war das denn für ein Linker-Error?

    Okay, der war folgender:

    **[Linker Fehler] Unresolved external '__stdcall DPInit(char , char )' referenced from Z:\5085XX\508957_MSIA\SOFTWARE\BUMASO\BUMASO 0.6\TPROFIBUS.OBJ
    Wobei meine TProfibus.cpp so aussieht:

    //---------------------------------------------------------------------------
    #include <vcl.h>
    #ifdef _WIN32
        #include <windows.h>
    #endif
    #include "prof_dp.h"
    #include "TProfibus.h"
    #include "Tools.h"
    #include "MainForm.h"
    // local variables
    static HANDLE					ConsoleOutput;	// console output
    static CONSOLE_SCREEN_BUFFER_INFO		ConsoleInfo;	// current console info
    static UINT					uNotifyMessage;	// notify message
    __declspec(dllimport) EXPO1 DP_RESULT EXPO2 DPInit(LPSTR pDllName, LPSTR pInitSting);
    
    // prototypes
    static bool InitCommunication(unsigned int uPort);
    static bool StopCommunication(void);
    static bool GetAnswer(void);
    static bool DpeGetAnswer(void);
    static bool ReceiveMessages(void);
    static bool OperationMenu(void);
    static bool DpeOperationMenu(void);
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    //---------------------------------------------------------------------------
    __fastcall TProfibus::TProfibus()
    {
    	ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    	if (!GetConsoleScreenBufferInfo(ConsoleOutput, &ConsoleInfo))
    	{
    
    	}
    }
    //---------------------------------------------------------------------------
    void __fastcall TProfibus::Init(int SlaveID_int, unsigned int uPort)
    {
    if (!InitCommunication(uPort))
    	{
    	ShowMessage("lauft nöd");
    	}
    else
            {
            ShowMessage("Communication Started");
            // Alle Components auf dem Formular enablen
            Tools::SetAllComponents(Main_Frm, true, crArrow);
            }
    }
    //---------------------------------------------------------------------------
    static bool InitCommunication(unsigned int uPort)
    {
    #if USE_THREAD
    	DWORD			dwThreadID;		// id of current thread
    #endif
    	char			Buffer[20];		// text buffer
    
    	ShowMessage("COM" + uPort);
    #if 1
    	if (DPInit(NULL, Buffer) != DP_NO_ERROR)
    #else
    	if (DPInit("fdlcp", NULL) != DP_NO_ERROR)		// open SIEMENS Profibus card
    #endif
    	{
    		ShowMessage("Unable to init DP");
    		return FALSE;
    	}
    									// get notification message
    #if USE_THREAD
    	uNotifyMessage = RegisterWindowMessage("DrvDemoMessage");
    	if (!uNotifyMessage)
    	{
    		ShowMessage("Unable to register message");
    		return FALSE;
    	}
    									// notify by thread ID
    	dwThreadID = GetCurrentThreadId();
    									// notify only acyclic operations.
    	if (DPSetUserMsg(NULL, dwThreadID, uNotifyMessage, MSG_TYPE_ASYNC /*| MSG_TYPE_DPCYCLE */ | MSG_TYPE_DPE) != DP_NO_ERROR)
    	{
    		ShowMessage("Unable to set user message");
    		return FALSE;
    	}
    									// enable communication thread
    	if (DPEnableCommThread(TRUE) != DP_NO_ERROR)
    	{
    		StopCommunication();
    		ShowMessage("Unable to init thread");
    		return FALSE;
    	}
    #endif
    
    	return TRUE;
    }
    
    //---------------------------------------------------------------------------
    

    [edit]code hinzugefügt[/edit]



  • hallo,

    also das enum ist bullshit.

    schreib mal

    enum EnTDPResult //und hier kein gleichzeichen.
    { //geschweifte klammer
    ...

    }; //geschweifte klammer und semikolon

    dann übersetzt das auch der compiler und du hast schon mal ein problem weniger.

    mfg
    murph



  • murphy schrieb:

    hallo,

    dann übersetzt das auch der compiler und du hast schon mal ein problem weniger.

    Das ist richtig, der Linker motzt aber immernoch:
    **[Linker Fehler] Unresolved external '__stdcall DPInit(char , char )' referenced from Z:\5085XX\508957_MSIA\SOFTWARE\BUMASO\BUMASO 0.6\TPROFIBUS.OBJ


Anmelden zum Antworten