Calling a DLL

This is a discussion on Calling a DLL within the C++ Programming forums, part of the General Programming Boards category; Hi I'm using Visual Studio 2005. I have a simple Win 32 console app that I'm trying to explicitly call ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Calling a DLL

    Hi

    I'm using Visual Studio 2005. I have a simple Win 32 console app that I'm trying to explicitly call a 3rd party dll from.

    I can call the dll if I save my file that calls the dll as a "C" program. However, it does not work if I try to save it as a "CPP" file. I need to save it as a cpp file. I have two questions:
    First, is it possible to save the file in C and than call the function in a CPP program. The second question shows what I've tried along with the error I'm getting:

    I've placed the dll and the library in my system 32 folder. Here's what I have:
    Code:
    // test1.cpp : Defines the entry point for the console application.
    
    #include "stdafx.h"
    
    #include <windows.h>
    
    #include <iostream>
    
    char cSmilesPass[25];
    
    char cChemical[1000];
    
    char EstLin[1000];
    
    char EstNon[1000];
    
    char EstUlt[1000];
    
    char EstPrim[1000];
    
    char UltTime[25];
    
    char PrimTime[25];
    
    char EstMitiLin[1000];
    
    char EstMitiNon[1000];
    
    char DetailResults[12000];
    
    char numLines[1000];
    
    char ErrorMess[12000];
    
    int CallBIOWDLL(void)
    
    { 
    
    /* get handle to dll */
    
    HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\WINDOWS\\system32\\BIOWDLL.dll");
    
    /* get pointer to the function in the dll*/
    
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL), "GetSrcBIOWIN");
    
    /* Define the Function in the DLL for reuse. This is just prototyping
    
    the dll's function. A mock of it. Use "stdcall" for maximum compatibility.
    
    */ 
    
    typedef int (__stdcall * pICFUNC)(char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*,char*);
    
    pICFUNC MyFunction;
    
    MyFunction = pICFUNC(lpfnGetProcessID);
    
     
    
    /* The actual call to the function contained in the dll */ 
    
    int intMyReturnVal = MyFunction("test","test2",EstLin,EstNon,EstUlt,EstPrim,UltTime,PrimTime,EstMitiLin,EstMitiNon,DetailResults,numLines,ErrorMess);
    
    /* Release the Dll */
    
    FreeLibrary(hGetProcIDDLL);
    
    /* The return val from the dll */
    
    return intMyReturnVal;
    
    }
    
     
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
    
    return 0;
    
    }
    When I try to compile, I get the following error:

    Compiling...

    test1.cpp

    c:\documents and settings\epajn\my documents\visual studio 2005\projects\test1\test1\test1.cpp(42) : error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char [32]' to 'LPCWSTR'

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    Build log was saved at "file://c:\Documents and Settings\epajn\My Documents\Visual Studio 2005\Projects\test1\test1\Debug\BuildLog.htm"

    test1 - 1 error(s), 0 warning(s)

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I've been struggling with this for way too long. I will be extremely grateful for any pointers.




    --------------------------------------------------------------------------------
    James

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Figured it out

    The LPCWSTR in the error message implies that you compile your code for unicode, so the functions expect wide chars instead of chars. There're two ways to fix this error: the first is to use L"Foo" instead of "Foo" for each string literal (or for strings that need to be wide chars irrelevant of compiling mode). The other possibility is to use _T("Foo") which will prefix the literal with L modifier if required.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Calling Question
    By mercury529 in forum Windows Programming
    Replies: 11
    Last Post: 01-09-2007, 06:15 PM
  2. problem in calling DLL
    By bhagwat_maimt in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 10:43 PM
  3. Troubles Calling a DLL
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2005, 12:00 PM
  4. Calling a VB DLL w/ forms from C++ DLL
    By UW_COOP in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2003, 09:04 AM
  5. Function Calling From A DLL In a DLL
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 06-03-2002, 09:27 AM

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