Announcement

Collapse
No announcement yet.

Zugriff auf DLL Datei - bis jetzt konnte keiner helfen -

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Zugriff auf DLL Datei - bis jetzt konnte keiner helfen -

    Ich sollte aus einer DLL Datei <B>int</B>-Variable auslesen.<BR>
    <B>ES KLAPPT ABER NICHT</B>

    1. In der DLL Datei wird zehn mal eine Zufallzahl generiert.
    <PRE>
    /* MyDll.c Datei */
    #include <stdio.h>
    #include <stdlib.h>
    #include <dos.h>
    <P>
    __declspec(dllexport) int zufallZahl;
    <P>
    int main(void) {
    <P>
    int i;
    for(i=0; i<10; i++) {
    zufallZahl = rand()%100;
    _sleep(1);
    }
    <P>
    return 0;
    }
    </PRE>
    2) Die andere App.(MyDll_Test) sollte jetzt diese Zufallzahl aus der MyDll.dll Datei auslesen.
    <PRE>
    /* MyDll_Test.c Datei*/
    #include <stdio.h>
    #include <dos.h>
    #include <windows.h>
    <P>
    typedef int (*MYDLLINT)(int);
    MYDLLINT int_Var;
    <P>
    void myDll_init() {
    HINSTANCE dll;
    dll = LoadLibrary("MyDll");
    <P>
    if(dll == NULL)
    printf("\n DLL nicht geladen \n");
    <P>
    int_Var = (MYDLLINT)GetProcAddress(dll, "zufallZahl");
    <P>
    if(int_Var == NULL)
    printf("\n ZufallZahl nicht gelesen \n");
    }
    <P>
    int main() {
    int i;
    myDll_init();
    <P>
    for(i=0; i<20; i++) {
    printf("\n Zufallzahl aus DLL Datei %i = %i", i, int_Var);
    _sleep(1);
    }
    <P>
    return 0;
    }
    <PRE>
    <B>FAZIT</B>
    Die beiden Dateien lasen sich ohne Fehler Kompilieren -
    die INT-Variable wird aber aus der DLL Datei nicht gelesen("ZufallZahl nicht gelesen") - wo liegt der Fehler ???

    Besten Dank für jeden Tipp!
    christine.

    PS:
    als Compiler benutze ich Borland(BCC 5.5) free C/C++ commandline tools.
    <P>

  • #2
    Hallo,<BR>
    'int_Var' ist keine Variable sondern eine Funktion, die ein Int-Variable als Parameter erwartet und eine Int-Variable liefert. Am besten sollte man in der DLL eine Funktion schreiben, die die globale Variable 'zufallZahl' liefert. <BR>
    <BR>
    Gruß Pete

    Comment


    • #3
      Hi Peter,

      erstmal DANKE.
      Wie sollte ich die Funktion definieren?

      christine

      Comment


      • #4
        Hi,<BR>
        <BR>
        ich hab mal dein Code kurz überarbeitet.<BR>
        <BR>
        Gruß Peter<BR>
        <BR>
        <BR>
        /* MyDll.c Datei */<BR>
        #pragma hdrstop<BR>
        #include <windows.h><BR>
        #include <stdio.h><BR>
        #include <stdlib.h> <BR>
        #include <dos.h> <BR>
        <BR>
        int zufallZahl;<BR>
        <BR>
        int __declspec(dllexport) get_zufallZahl()<BR>
        {<BR>
        return zufallZahl;<BR>
        }<BR>
        <BR>
        int zufallZahl;<BR>
        <BR>
        #pragma argsused<BR>
        int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)<BR>
        {<BR>
        zufallZahl = rand()%100;<BR>
        return 1;<BR>
        }<BR>
        <BR>
        <BR>
        #pragma hdrstop<BR>
        <BR>
        /* MyDll_Test.c Datei*/<BR>
        #include <stdio.h><BR>
        #include <dos.h><BR>
        #include <windows.h><BR>
        <BR>
        typedef int (*MYDLLINT)();<BR>
        MYDLLINT get_int_Var;<BR>
        <BR>
        bool myDll_init()<BR>
        {<BR>
        HINSTANCE dll = LoadLibrary("MyDll");<BR>
        <BR>
        if(dll == NULL)<BR>
        printf("\n DLL nicht geladen \n");<BR>
        <BR>
        get_int_Var = (MYDLLINT)GetProcAddress(dll, "_get_zufallZahl");<BR>
        <BR>
        if(get_int_Var == NULL)<BR>
        printf("\n ZufallZahl nicht gelesen \n");<BR>
        <BR>
        return get_int_Var;<BR>
        }<BR>
        <BR>
        <BR>
        #pragma argsused<BR>
        int main(int argc, char* argv[])<BR>
        {<BR>
        int i;<BR>
        if ( myDll_init() )<BR>
        {<BR>
        for(i=0; i<20; i++)<BR>
        {<BR>
        printf("\n Zufallzahl aus DLL Datei %i = %i", i, get_int_Var() );<BR>
        _sleep(1);<BR>
        }<BR>
        }<BR>
        return 0;<BR>
        }<BR&gt

        Comment


        • #5
          Ich DANKE Dir! es funkt

          Comment

          Working...
          X