CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2013
    Posts
    1

    Getting Unicode from an API DLL using non Unicode program

    I have some code that was compiled without Unicode turned on in the Preprocessor Definitions. I need to access an API that had Unicode turned on in the Preprocessor Definitions (I believe that it is on by default for DLL’s) .
    I need to call a function in the DLL that requires a structure like:
    struct READERINFO
    {
    TCHAR serial[32];
    TCHAR altSerial[32];
    TCHAR name[32];
    TCHAR fccId[48];
    TCHAR hwVersion[16];
    int swVerMajor;
    int swVerMinor;
    char devBuild;
    };

    It returns some information in the structure some of it is Unicode based however the program that is calling it is not Unicode. The preprocessors are not turned on because if they were there would be a lot of things to change in this code. The code is old code that I inherited and now I must interface to some new devices.
    I declare my structure as :
    READERINFO info;
    Then I call the function in the DLL which looks like:
    ApiGetReaderInfo(hAPI, &info, sizeof(into));
    Which is defined as:
    ApiGetReaderInfo(HANDLE hApi,
    Struct READERINFO * ri,
    DWORD riSize);

    Parameters:
    hApi Handle to valid Api object instance
    ri Pointer to the READERINFO structure.

    riSize Size of ri structure in bytes. Usually: sizeof(struct READERINFO).

    When I call it from my program that does not have UNICODE defined in the Pre-Processors I get :
    Characters like : ÌÌÌÌÌ in the TCHAR fields and invalid numbers in the integer fields.
    int ModuleVersion(HANDLE hApi)
    {
    struct READERINFO info;
    ApiGetReaderInfo(hApi, &info, sizeof(info));
    //Process data
    sprintf(msgbuf,"Module Version: [%d.%d-%c]\r\n\r\n",LPSTR(info.swVerMajor),LPSTR(info.swVerMinor),info.devBuild);
    MessageBoxA(NULL,msgbuf,"Win32 Application",MB_OK|MB_ICONEXCLAMATION);
    } 
    When I call it from my program that has some sample code just for this and has the UNICODE defined in the Preprocessors it works just fine.
    Any help would be appreciated on how I can call this from my old code and get the correct information.
    I have already tried to do the follow without success:

    int ModuleVersion(HANDLE hApi)
    {
    #define UNICODE
    struct READERINFO info;
    #undef UNICODE
    ApiGetReaderInfo(hApi, &info, sizeof(info));
    //Process data
    sprintf(msgbuf,"Module Version: [%d.%d-%c]\r\n\r\n",LPSTR(info.swVerMajor),LPSTR(info.swVerMinor),info.devBuild);
    MessageBoxA(NULL,msgbuf,"Win32 Application",MB_OK|MB_ICONEXCLAMATION);
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Getting Unicode from an API DLL using non Unicode program

    1) Please use code tags when posting code.

    2) You don't convert strings by merely casting them to LPSTR as you're doing now. That does absolutely nothing except make the compiler suppress errors (which you should have taken note of).

    You need to actually convert them by using the MultiByteToWideChar / WideCharToMultiByte functions.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Getting Unicode from an API DLL using non Unicode program

    In addition to that:
    Code:
    [%d.%d-%c]\r\n\r\n",LPSTR(info.swVerMajor),LPSTR(info.swVerMinor),
    You are specifying an integer format, but supplying a pointer. That's another reason why your output may look strange.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Getting Unicode from an API DLL using non Unicode program

    First of all, turn on UNICODE in your project. It has no sense to keep hooked in ANSI strings.
    Have a look in this FAQ: Which Windows API functions are faster, ANSI or UNICODE?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Getting Unicode from an API DLL using non Unicode program

    Code:
    #define UNICODE
    #define _UNICODE
    
    #include "your unicode dll api.h"
    
    #undef UNICODE
    #undef UNICODE
    HOWTO_call_unicode_dll_from_ansi_exe.zip
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured