-
August 22nd, 2018, 10:26 AM
#1
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
-
August 22nd, 2018, 11:49 AM
#2
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.9.7)
-
August 22nd, 2018, 04:36 PM
#3
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.
-
August 24th, 2018, 09:23 AM
#4
Re: Help on calling DLL
Frome the command line, do a dumpbin /exports on the dll and see what the exported function signatures are.
-
August 24th, 2018, 02:13 PM
#5
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++.
-
August 27th, 2018, 02:56 PM
#6
Re: Help on calling DLL
 Originally Posted by caperover2000
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|