Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
How To Create A .Dll in Visual C++ Express 2008

Reply  
Thread Tools
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
16-Apr-2009, 09:39 AM #16
Sorry, I should have added more information. I initially asked how to create .Dll's as they seemed a good way to modularize my code, making it easier to handle, and because most programs seem to use them. After completing the initial examples given to me by Mt2002, I started looking at what else .Dll's could do. My research indicated that they could be used for global hooks, which I though would be usefull for a global hotkey program, however the documentation is unclear as to which functions go where, and my code does not produce any errors or give any output. Below is the code for the program.

Code:
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;


int main(){
    HOOKPROC KeyProc;
    HINSTANCE MyDll;
    HHOOK MyHook;

    MyDll = LoadLibrary("GlobalHotkey.dll");
    if (MyDll == NULL){
        cout << "MyDll: Loading Failed" << endl;
        return -1;
    }

    KeyProc = (HOOKPROC)GetProcAddress(MyDll, "KeyboardProc@12");
    if (KeyProc == NULL){
        cout << "KeyProc: Loading Failed" << endl;
        return -1;
    }

    MyHook = SetWindowsHookEx(WH_KEYBOARD,KeyProc,MyDll,0);
    if (MyHook == NULL){
        cout << "KeyProc: Loading Failed" << endl;
        return -1;
    }

    cout << "MyDll is loaded at: " << MyDll << endl;
    cout << "KeyProc is loaded at: " << KeyProc << endl;
    cout << "MyHook: " << MyHook << endl;

    cin.ignore();
}
And this is the code for my .Dll

Code:
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

fstream KeyInfo;

extern "C" __declspec(dllexport) void MainFunction(){
    cout << "Dll: Main Function" << endl;
}

extern "C" __declspec(dllexport) LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam){

    cout << "Code: " << code << endl;
    cout << "WPARAM: " << wParam << endl;
    cout << "LPARAM: " << lParam << endl;

    KeyInfo.open ("C:\\KeyInfo.txt", fstream::in | fstream::out | fstream::app);
    KeyInfo << "Code: " << code << endl;
    KeyInfo << "WPARAM: " << wParam << endl;
    KeyInfo << "LPARAM: " << lParam << endl;

    return CallNextHookEx(0, code, wParam, lParam);
}
As stated before, the above code does not produce any errors, neither does it produce any output. Can you see anything wrong with the code, or where the functions are ? Thanks -Fabez-
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
mt2002's Avatar
Computer Specs
Senior Member with 926 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
17-Apr-2009, 02:01 PM #17
Quote:
I started looking at what else .Dll's could do. My research indicated that they could be used for global hooks
.
They can be, yes. The most common method that I have seen and used them for is an extension to the original program. ie, kind of like a plugin library to extend the functionaily of the original program.

To make things simple for now, lets focus on you being able to call the MainFunction function from the DLL. You do not have to use the symbolic name for the function. So, to get the address of MainFunction, just use GetProcAddress (MyDll, "MainFunction");

Please let me know if it gets called.
__________________
char c[3]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
17-Apr-2009, 02:17 PM #18
Using the following code, the MainFunction executed without any errors.

Code:
FARPROC Something = GetProcAddress(MyDll, "MainFunction");
if (Something == NULL){
    cout << "MainFunction: Loading Failed" << endl;
    return -1;
}
(Something)();
Just a quick question though, what does FARPROC stand for ?
mt2002's Avatar
Computer Specs
Senior Member with 926 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
17-Apr-2009, 07:20 PM #19
Quote:
Just a quick question though, what does FARPROC stand for ?
Code:
typedef int (FAR WINAPI *FARPROC)()
It identifies a function pointer of the form int (_stdcall *FARPROC) () (FAR is defined as either nothing or the far keyword on some older compiliers that support far (32 bit linear) pointers, and WINAPI is defined as _stdcall calling convention in most cases.)

In your code, it creates a function pointer, something that points to the function from within the loaded DLL to be called.

k, we have verified that MainFunction right now works? If so, we can focus on the other routine. I am still uncertain if that will work, however, as you are using a C++ calling convention but exporting it as a C routine. I'll have to double check that one...
__________________
char c[3]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
burnthepc's Avatar
Computer Specs
Senior Member with 305 posts.
 
Join Date: Aug 2007
Location: London, UK
Experience: Intermediate
19-Apr-2009, 11:40 AM #20
Just wanted to say, great post mt2002!

I've wondered how it's done
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
19-Apr-2009, 11:42 AM #21
Agreed, it certainly helped
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
24-Apr-2009, 06:42 PM #22
I have yet another question for you If I have the string containing a function name, is it possible to execute the function contained in the string ? Also how do I check if I have read or write permissions to a certain area, such as the users My Documents area to create a settings folder and to create and output folder for the screenshots.
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
mt2002's Avatar
Computer Specs
Senior Member with 926 posts.
 
Join Date: Sep 2005
Location: 127.0.0.1
Experience: Advanced
25-Apr-2009, 09:01 AM #23
Quote:
Originally Posted by -Fabez- View Post
I have yet another question for you If I have the string containing a function name, is it possible to execute the function contained in the string ?
You can only indirectly call a function by either i) import/export tables or ii) directly via function pointers (GetProcAddress does this by a function name lookup via the tables) If the only information that you have is the function name, then no you cannot do it directly unless you use one of the above methods.

Quote:
Also how do I check if I have read or write permissions to a certain area, such as the users My Documents area to create a settings folder and to create and output folder for the screenshots.
This one I am not to sure of...Im sure there is something in the Win32 API for it though. Ill post it if I find anything...
__________________
char c[3]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:34 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.