Avatar of deming
deming

asked on 

How to call C++ LPTStr in C#

I have a DLL written in C++ and I am trying to call it from C#.

Here is my C++ DLL:
-----------------------------
extern "C" __declspec(dllexport) BOOL WINAPI MYTEST(LPTSTR PW, DWORD BlockNumber, DWORD Date, WORD Options, DWORD ProductCode)

Here is what I tried in C#:
----------------------------------
            UInt32 ID = 654321;
            UInt32 ExpireDate = 20090228;
            UInt16 Options = 0;
            UInt32 ProductCode = 1;
            String MyPassword = "                                                               ";
            Class1.MYTEST(MyPassword, ID, ExpireDate, Options, ProductCode);

Where Class1.MYTEST is:
--------------------------------------
        [DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
        public static extern bool MYTEST([MarshalAs(UnmanagedType.LPTStr)] string password, UInt32 ID, UInt32 Date, UInt16 Options, UInt32 ProductCode);

PROBLEM: MyPassword is being returned as Chinese characters but they should be ANSI.  What did I do wrong?
.NET ProgrammingC#C++

Avatar of undefined
Last Comment
deming

8/22/2022 - Mon