DLL problem

This is a discussion on DLL problem within the C++ Programming forums, part of the General Programming Boards category; in DRAW.dll Code: #include <windows.h> #include <iostream> using namespace std; int main() { HINSTANCE theDLL; theDLL = LoadLibrary("DRAW.dll"); __declspec(dllimport) void ...

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    DLL problem

    in DRAW.dll
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	HINSTANCE theDLL;
    	theDLL = LoadLibrary("DRAW.dll");
    	__declspec(dllimport) void Hello();
    	cin.get();
    }
    in program
    Code:
    #include<iostream>
    #include <windows.h>
    
    using namespace std;
    
    __declspec(dllexport) void Hello()
    {
        MessageBox(NULL,"godd's DLL :D","message",MB_OK);
    }
    the function doest seem to be called can anyone help me fix this...

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Probably because you don't call the function.

    __declspec(dllimport) void Hello();

    declares the function. Link to the DLL's lib file in your program - drop the LoadLibrary call too.

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    when i try to call the function i get this error
    Code:
    --------------------Configuration: DWSW - Win32 Debug--------------------
    Compiling...
    main.cpp
    C:\C++\DWSW\main.cpp(12) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Linking...
    main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl Hello(void)" (__imp_?Hello@@YAXXZ)
    Debug/DWSW.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    
    DWSW.exe - 2 error(s), 1 warning(s)

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Here is a very basic dll demo that works. See if you can see what I did and then compare that to what you did (heres a hint: if something's different you are probably wrong).

    Here is the dll file code:
    Code:
    #ifdef __cplusplus
    #define EXPORT extern "C" __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllexport)
    #endif
    
    #include <windows.h>
    
    EXPORT void doBeep(void);
    
    int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved ) {
    
    	return true;
    }
    
    EXPORT void doBeep(void) {
    
    	for(int i = 0; i<5; i++)
    		MessageBeep(0);
    
    }
    Here is the def file code

    Code:
    LIBRARY	DLLdemo
    EXPORTS
    	doBeep
    And finnally here is the code for the test app:

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    //define my pointer type to the function---------------------------------------
    typedef int(*myProc)(void);
    myProc proc = NULL;
    //-----------------------------------------------------------------------------
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
    
    	static TCHAR szAppName[] = TEXT("dllTest");
    	HWND hwnd;
    	MSG msg;
    	WNDCLASS wc;
    	
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = szAppName;
    	wc.lpszMenuName = NULL;
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    
    	if(!RegisterClass(&wc)){
    		MessageBox(NULL, TEXT("Could not register class"), szAppName, MB_ICONERROR);
    		return (0);
    	}
    
    	hwnd = CreateWindow(szAppName, TEXT("dllTEST"), WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, hInstance, NULL);
    
    	ShowWindow(hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&msg, NULL, 0, 0)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam ;
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    
    	static HINSTANCE hInstLib; //handle to the library
    	
    	
    	BOOL fFreeResult;
    
    	switch(message) {
    			
    		case WM_CREATE:
    			
    			hInstLib = LoadLibrary("Dlldemo.dll"); //Load the dll
    			proc = (myProc)GetProcAddress(hInstLib, TEXT("doBeep"));//Get address of function
    			proc();//call function
    			return(0);
    		case WM_DESTROY:
    			fFreeResult = FreeLibrary(hInstLib); //release library		
    			PostQuitMessage(0);
    			return(0);
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    This should get you going in the right direction.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Thanks your example was a great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  2. Replies: 1
    Last Post: 09-18-2005, 10:06 PM
  3. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  4. std::string vs char* DLL problem
    By aker_y3k in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2002, 10:05 AM
  5. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 11:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21