Announcement

Collapse
No announcement yet.

dll problem

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

  • dll problem

    hallo, ich habe ein dll problem. die situation ist folgende: ich habe ein programm und eine dll mit globalen variablen. ungefähr so:

    <pre>
    // --------------------- die dll ---------------------
    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop

    extern "C" __export void SetGlobalVar1(int Value);
    extern "C" __export void SetGlobalVar2(char Value);

    int GlobalVar1;
    char GlobalVar2;

    #pragma argsused

    int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
    {
    return 1;
    }

    void SetGlobalVar1(int Value)
    {
    GlobalVar1 = Value;
    }

    void SetGlobalVar2(char Value)
    {
    GlobalVar2 = Value;
    }
    </pre>

    das programm ist nun im vordergrund (auf dem desktop), lädt die dll, sucht sich die functionen raus und ruft zb SetGlobalVar1(14) auf. dann hat GlobalVar1 den wert 14. wenn das jetzt so bleiben würde, wär alles gut. aber es bleibt nicht so, denn sobald das programmm nicht mehr im vordergrund ist, haben alle variablen den wert null (GlobalVar1 = 0, GlobalVar2 = 0). wenn das programm wieder in den vordergrund kommt, werden die alten werte wieder hergestellt. und ich hab absolut keine ahnung, warum das nicht functioniert!

    ich wär extreeem dankbar, wenn mir jemand hilft. ich hab desswegen schon zwei tage verbracht (weil ich erst nicht wusste, das die vars auf null gesetz werden... und dachte desswegen, da sind imma irgendwelche fehler)

    und vielleicht kann mir ja auch noch jemand sagen, ob man aus der dll eine function vom programm aufrufen kann (wenn man der dll während des laufens die addresse der function sagt).

    bitte bitte bitte helft mir

    thx sandman

  • #2
    Hi,

    ich habe es damlas ausprobiert von der DLL den Caption eines Labels zu ändern. Dieses hatte mit dem zeiger funktioniert, also wird es wohl auch mit einer Funktion funktionieren. Ich kann dein Fehler nicht ganz nach vollziehen. Ist es vielleicht möglich, das du die DLL ausversehen mehrere male neu lädst und deswegen die Werte der Variablen immer 0 sind?
    Der C++ Builder ist ja manchmal recht empfindlich du kannst ja mal ausprobieren die Variablen etwas anders zu plazieren.

    int GlobalVar1;

    char GlobalVar2;

    extern "C" __export void SetGlobalVar1(int Value);

    extern "C" __export void SetGlobalVar2(char Value);

    Keine Garantie, aber vielleicht läuft es ja dann, denn ich hatte schon ähnliche Situationen gehabt.

    Ciao Philip

    Comment


    • #3
      hi. ich habe jetzt rausgefunden, warum es nicht geht. mir ist aber immer noch rätselhaft, warum das so ist. ich benutze die dll ja, um eine hook function unterzubringen (die man ja nur in eine dll schreiben darf). es ist ein keyboad hook. wenn mein fenster im vordergrund ist und ich tasten drücke, alles schön und gut. wenn ein anderes fenster allerdings den focus (tastaturereignisse) erhält und ich eine taste drücke, dann wird in meiner dll DllEntryPoint(..) aufgerufen, und reason hat den wert DLL_PROCESS_ATTACH. dabei werden natürlich alle variablen wieder neu für die instanz initialisiert (-> auf null gesetzt). aber warum?? warum wird die dll dann noch einem process zugeordnet?? die hook function wird dann natürlich in der jeweiligen instanz aufgerufen (-> der hook fürt nur zum erfolg, wenn mein fenster auch den focus hat... allerdings könnte ich dann auch gleich die WM_KEYDOWN messages für mein fenster verarbeiten).

      (

      biitteee.. hiilfee.. fleehh

      sandman

      ps: vielleicht ist das jo sorag richtig so. wenn ja, kann mir dann jemand sagen, wie alle instanzen auf die selben variablen zugreifen können

      Comment


      • #4
        Hi,

        ein ähnliches Problem hatt eich auch mal gehabt. Der Hook funktionierte bei mir nur, wenn mein Fetnster aktiv war. Bei mir lag es daran, das ich die Variablen etwas schlecht belegt habe. Sobald eine globale Variable mit einem Wert gefüllt werden sollte ging es nur, wenn mein fesnter aktiv war, anderfalls ging es nicht. Versuch doch einfach mittels SendMessage die Informationen aus dem Hook in deine Anwendung zu schicken. Mit Hooks hat man jedoch immer Probleme.

        Ciao Philip

        Comment


        • #5
          hi. an sich keine schlechte idee. aber das hab ich schon ausprobiert. um eine message an das fenster zu senden brauch ich doch aber das fensterhandle -> variable -> probleme ich hab jetz ma nachgeguckt, wie man in einer .def datei neue datensegmente deklarieren kann, die shared sind. allerdings weiss ich nich, wie man dem borland compiler sagt, wo ein segment anfängt und wo es aufhört (in vc is das #pragma data_seg("name")). weisst du das?

          cu sandman

          ps: kannst du mir vielleicht mal deinen code zeigen? wär nett. ich bin nämlich schon kurz vorm verzweifeln. :

          Comment


          • #6
            Hi,

            ich habe hier ein Stcük Code aus den Newsgroups gefunden, welcher auch SendMessage verwendet. Vielleicht hilft er dir ja weiter

            <pre>

            > I'd like to log all keystrokes while the program is running. How can I do
            > this with API-calls? My OS is Win98.

            This question gets asked over and over again so here I'll provide a
            complete code example:

            First, start a new project group (File | New | Project Group). Add to it
            an application project (I've called it "KeyHook") and a DLL project
            ("KeyDLL" for example). Save all files. Switch to the KeyHook project.
            Name the main form "KeyHookForm" and add to it a ListBox.

            In the header file of your main unit (the header of KeyHookForm), insert:

            #define WM_KEYHOOK WM_USER+100

            // ...

            private: // User declarations
            MESSAGE void __fastcall KeyHook(TMessage &);
            BEGIN_MESSAGE_MAP
            MESSAGE_HANDLER(WM_KEYHOOK, TMessage, KeyHook);
            END_MESSAGE_MAP(TForm);

            To do source code of the unit add:

            extern "C" __declspec(dllexport) __stdcall void SetHook(void);
            extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
            extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,
            LONG);

            void __fastcall TKeyHookForm::KeyHook(TMessage &Message)
            {
            char Key[80];
            GetKeyNameText(Message.LParam, Key, 80);
            ListBox1->Items->Add(Key);
            }

            Edit the OnCreate event of your form:

            void __fastcall TKeyHookForm::FormCreate(TObject *Sender)
            {
            SetHook();
            }

            Edit the OnDestroy event of your form:

            void __fastcall TKeyHookForm::FormDestroy(TObject *Sender)
            {
            RemoveHook();
            }

            Now switch to your DLL project. C++Builder has probably created for you
            a KeyDLL.cpp file. Change the contents of this file to:

            #include <vcl.h>
            #include <windows.h>
            #pragma hdrstop
            #define WM_KEYHOOK WM_USER+100

            HHOOK ghhookKB;
            HINSTANCE ghInst;

            int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
            {
            ghInst = hinst;
            return (1);
            }

            extern "C" __declspec(dllexport) __stdcall void SetHook(void);
            extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
            extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,
            LONG);

            void __stdcall SetHook(void)
            {
            HOOKPROC lpfnHookProc = NULL;
            lpfnHookProc = GetProcAddress(GetModuleHandle("KEYDLL.DLL"),
            "CheckKey");
            ghhookKB = SetWindowsHookEx(WH_KEYBOARD, lpfnHookProc, ghInst, NULL);
            }

            void __stdcall RemoveHook(void)
            {
            UnhookWindowsHookEx(ghhookKB);
            }

            DWORD __stdcall CheckKey(int nCode, WORD wParam, LONG lParam)
            {
            HWND ghAppWnd = FindWindow("TKeyHookForm", 0);

            if((nCode < 0) || nCode == HC_NOREMOVE)
            return CallNextHookEx(ghhookKB, nCode, wParam, lParam);

            // Skip if it's a repeat
            if(lParam & 0x40000000)
            return CallNextHookEx(ghhookKB, nCode, wParam, lParam);

            // Send key information to the main window
            SendMessage(ghAppWnd, WM_KEYHOOK, 0, lParam);

            return CallNextHookEx(ghhookKB, nCode, wParam, lParam);
            }

            Compile the KeyDLL project. Switch back to the KeyHook project. Add to
            it the KeyDLL.lib file (Project | Add to Project - KeyDLL.lib). Compile
            and run KeyHook.

            If everything went fine, each time a key is pressed, no matter which
            application has focus, the key name will be added to the ListBox.

            --
            C++Builder Developer's Network
            http://www.cbdn.cjb.net/

            </pre>

            Dann wünsche ich dir noch viel Glück zum Erfolg.

            Ciao Philip

            Comment


            • #7
              danke. aber ich hab die dll jetzt mit visual c geschrieben (ii micro$oft).... und jetzt gehts!!

              cu sandma

              Comment

              Working...
              X