dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help on calling DLL

  1. #1
    Join Date
    Apr 2011
    Posts
    45

    Help on calling DLL

    Hi, All,

    I have a third party dll, which has been used in C#, and now I want to call it from native C++. I am have some troubling making it work. Could you someone please help?

    Two functions are defined and used in C# as follows:

    [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    public static extern int FT_ifinDLogin(string strUserName, string strPassWord, int nType);

    [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    unsafe public static extern int FTQueryTHS_SynHisQuoteC(string THSCodes,
    string jsonIndicator,
    string jsonParam,
    string begintime,
    string endtime,
    out char* pret
    );

    static int THS_iFinDLogin(string account, string password)
    {
    return FT_ifinDLogin(account, password, 5);
    }

    unsafe static string THS_HistoryQuotes(string THSCodes,
    string jsonIndicator,
    string jsonParam,
    string begintime,
    string endtime
    )
    {
    string strOut;
    char* Pret;
    FTQueryTHS_SynHisQuoteC(THSCodes, jsonIndicator, jsonParam, begintime, endtime, out Pret);
    IntPtr data = new IntPtr(Pret);
    strOut = Marshal.PtrToStringUni(data);
    DeleteBuffer(Pret);
    return strOut;
    }


    In C++, I define the functions as follows

    typedef int (__stdcall *THS_IFinDLogin)(const wchar_t *, const wchar_t *, int)

    typedef int (__stdcall *THS_HistoryQuotes( const wchar_t *, const wchar_t *,const wchar_t *,const wchar_t *,const wchar_t *, wchar_t *)

    When calling them in C++, it always fails with an error "wrong request data".

    Could anyone help me out?

    Thanks!

    CR

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,479

    Re: Help on calling DLL

    I'm by no means an expert on c#, but in c# I understand that a string consists of a sequential collection of Char objects together with a Length property that represents the number of Char objects it contains, not the number of Unicode characters.

    This is completely different from c++. I think the answer in c++ is to use BSTR.

    See https://www.codeproject.com/articles...ng-conversions

    which although is for VB I think is also valid for c#.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  3. #3
    Join Date
    Apr 2011
    Posts
    45

    Re: Help on calling DLL

    Thanks for your suggestion. My understanding is that BSTR should be used when calling COM components, but the dll does not look like COM but native C functions instead.

    C# explicitly specify the Char Set as Unicode. I tend to think that is where the problem might be.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,801

    Re: Help on calling DLL

    Frome the command line, do a dumpbin /exports on the dll and see what the exported function signatures are.

  5. #5
    Join Date
    Apr 2011
    Posts
    45

    Re: Help on calling DLL

    dumpbin /exports does not tell all the information especially if a short alias is defined.
    I know the C# code is working fine and just want to figure out a way to replicate it in C++.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,801

    Re: Help on calling DLL

    Quote Originally Posted by caperover2000 View Post
    dumpbin /exports does not tell all the information especially if a short alias is defined.
    I know the C# code is working fine and just want to figure out a way to replicate it in C++.
    Do you know whether the dll is coded for ANSI or UNICODE? Looks probably to be UNICODE.
    Check out pinvoke.net for conversion ideas (the site will have examples of native c functions converted to c#. You can find similar params in c" and look at their native counterparts.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)