Avatar of CSecurity
CSecurityFlag for Iran, Islamic Republic of

asked on 

Calling my C++ DLL in C# - not works

I have a DLL in C++ and one of it's export's definations is like this:

void __stdcall MyFunc (const char *in, LPSTR result, size_t n, int iMode, char *Password)

I can't get it working PROPERLY (it works but with strange charachters, strange errors, strange behaviors)...


I defined it's Call method in C# like this:


[DllImport("AESDLL.dll")]
        private static extern void EncryptString(
            String cData,
            [MarshalAs(UnmanagedType.LPStr)] StringBuilder Output,
            int nSize,
            int iMode,
            [MarshalAs(UnmanagedType.LPStr)] StringBuilder Password
         );

I tried WITH 10 more methods, removed MarshalAS, not worked.

I tried Using String for all, not worked

I tried using StringBuilder for all not worked

Please advice, I get unrelated outputs...

My C++ DLL returns output in result variable. (LPSTR result - second variable)

Please advice, thanks from now!
.NET ProgrammingC++C#

Avatar of undefined
Last Comment
Minh Võ Công
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

ASKER

I should add that this defination worked well in VB6 for my DLL:

Private Declare Sub MyFunc Lib "MyDLL.dll" (ByVal cData As String, ByVal output As String, ByVal Size As Long, ByVal iMode As Integer, ByVal password As String)
if password is not change you can implement like:

[DllImport("AESDLL.dll",CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern void EncryptString(
            String cData,
            [MarshalAs(UnmanagedType.LPArray)]byte[] buffer,
            int nSize,
            int iMode,
            String pass
         );
const int MAX_SIZE = 1000000;
byte[] buffer = new byte[MAX_SIZE * 2];

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Minh Võ Công
Minh Võ Công
Flag of Viet Nam image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

ASKER

WHat do you mean by buffer? My function uses String


void __stdcall MyFunc (const char *in, LPSTR result, size_t n, int iMode, char *Password)


Output is in result
You can retrieve string by:

string str = System.Text.ASCIIEncoding.ASCII.GetString(buffer );
or
string str = System.Text.UnicodeEncoding.Unicode.GetString(buffer );
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo