Explicit DLL calls

This is a discussion on Explicit DLL calls within the Windows Programming forums, part of the Platform Specific Boards category; I'm using VC++ 2008 I've spent my day trying to learn about dll's and how to use them but there's ...

  1. #1
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question Explicit DLL calls

    I'm using VC++ 2008

    I've spent my day trying to learn about dll's and how to use them but there's a surprising lack of resources available on the internet.

    I've been following a tutorial and it's given this code.

    within my dll project i have a file called dllmain.cpp that says this:
    Code:
    // dllmain.cpp : Defines the entry point for the DLL application.
    #include "stdafx.h"
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
    					 )
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    	case DLL_THREAD_ATTACH:
    	case DLL_THREAD_DETACH:
    	case DLL_PROCESS_DETACH:
    		break;
    	}
    	return TRUE;
    }
    I have another .cpp file with the same name as my project in which I copied this from the tutorial:
    Code:
    #include "jkhg.h"
    
    
    // This is an example of an exported function.
    int MyDLLFunc2(char *TEXT) 
    {
    	MessageBox(NULL,TEXT,"",MB_OK);
    
    	return true;
    }
    jkgh.h contains:
    Code:
     int MyDLLFunc2(void);
    And I added a .def file that contains:

    Code:
    LIBRARY JKHG
    DESCRIPTION This is my DLL file!
    
    EXPORTS
        	MyDLLFunc2	@1

    The application that calls the dll contains this precompiled header:
    Code:
    #pragma once
    #include <windows.h>
    and the main cpp file contains this:

    Code:
    #include "stdafx.h"
    
    
    typedef UINT (CALLBACK* importFunc1)(char *TEXT);
    
    HINSTANCE hDLL;   
    importFunc1 MyFunc2;
    UINT  uReturnVal;
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        hDLL = LoadLibrary("c:\\jkhg.dll");
    
    	if (hDLL != NULL)
    	{
    		MyFunc2 = (importFunc1)GetProcAddress(hDLL,"MyDLLFunc2");
    
    		if (!MyFunc2)
    		{
    			//Show error message
    			FreeLibrary(hDLL);       
    			return false;
    		}
    		else
    		{
    			uReturnVal = MyFunc2("HELLO WORLD!");
    		}
    	}
    
    	return true;
    }
    I know this is a long post but I don't know which code is critical and which isn't.

    And now to my problem. I successfully load the dll from c:\jkhg.dll but now matter what I do myFunc2 always remains empty and this block of code in viI.cpp always executes:
    Code:
    if (!MyFunc2)
    		{
    			//Show error message
    			FreeLibrary(hDLL);       
    			return false;
    		}
    I'm gratefull for any amount of help or light-shedding.

    sorry about the lousy names, I tend to bang on the keyboard during tutorial readings.

  2. #2
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Remove the jkgh.h and put the code in dllmain.cpp You have conflicting declarations for the function as well, does it accept a char * or nothing. Also in your .h file you will probably need to declare the function as:
    Code:
    DLLIMPORT int void MyDLLFunc2(char *)
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    What's the point of moving it into the app? You also seem to confuse the reader somewhat with a non-existent macro that is not necessary.
    OK, let's begin. First, let's get rid of the annoying dotNet crap. Create a dll as usual (you seem to know how). Then create an app (Win32 -> win32 console, NOT Windows Forms or anything else).

    Note here. After creating the dll, you can right-click solution -> add -> project to create a new project and attach it to the solution.

    So create your function and put the declaration in a header.
    Now let's create a macro and put it there:

    Code:
    #ifdef DLL
    #define EXPORT __declspec(export)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    Put that macro before your declaration (ie EXPORT void foo()).
    Good. Now go back to your application.
    Include the header. Then call your function as if it were part of the exe.
    One last step!

    Open solution properties. Goto dependencies and select your app in the combobox. Then tick the box besides your dll. Accept changes. Compile and run.
    There you go. Easier, less error prone and working!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by Elysia View Post
    Code:
    #ifdef DLL
    #define EXPORT __declspec(export)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    Put that macro before your declaration (ie EXPORT void foo()).
    Should I put it in front of the definition as well?
    Also after I added that to the header file as if by magic a .lib file for my dll appeared.


    Quote Originally Posted by Elysia View Post
    Open solution properties. Goto dependencies and select your app in the combobox. Then tick the box besides your dll. Accept changes. Compile and run.
    There you go. Easier, less error prone and working!
    Doing what I think was following this I added the .lib file under additional dependencies in the linker tab. Was that correct? I also put the .dll in the debug directory of the application. That seemed obvious but it's the most obvious things that are teh easiest to mess up. (EDIT: like the spelling of "the" <--)

    my app now looks like this:
    Code:
    #include "jkhg.h"
    
    int main()
    {
        
    	MyFunc2("HELLO WORLD!");
    
    	return true;
    }
    using the jkhg.h file from the .dll project that looks like this:
    Code:
    #ifdef DLL
    #define EXPORT __declspec(export)
    #else
    #define EXPORT __declspec(dllimport)
    #endif 
    
    EXPORT int MyDLLFunc2(char *text);
    The .def file, dllmain.cpp file and jkhg.cpp file look the same.

    The dll compiles just fine, but the application give me the error :
    Code:
    'MyFunc2': identifier not found
    Which makes me think that I screwed up what you said on the dependencies thing.

    Also the add procet to the solution thing was VERY helpfull. Before I was using two seperate windows. Thanks for your help.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You can get rid of the .def file now. It is no longer needed!
    The macro is only necessary for the declarations, not the definition.
    I wasn't talking about the additional lib paths. Have a look at this screenshot: http://i201.photobucket.com/albums/a...pendencies.jpg

    Also forgot another detail: In project options, under preprocessor defines, for the dll, add DLL. This will make sure it exports the function properly.
    Try this and let's see what happens.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Thank you Elysia. After following your help it worked

    It turns out I had another simpler problem in that in my header file I told my app to import the function and call it MyDLLFunc2 but then in the cpp file I tried to call MyFunc2 instead.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Terrific. Then good luck to you on future endeavours!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Explicit DLL loading
    By true_organs in forum Windows Programming
    Replies: 6
    Last Post: 05-24-2008, 01:26 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM

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