0

I have a c# program that checks data base strings from a c++ dll.

I already read this pages :

My strings pass well with no error but my problem is they're not matching in C++ dll.

I tried to check them out with Messagebox , Console and Everything and they are same at characters , size , text ...

but If Else always returns false ...

My C++ code ( test_match.dll ) :

extern "C" __declspec(dllexport) int check_string(const char* string_from_csharp);
int check_string(const char* string_from_csharp)
{
    if (string_from_csharp == "hello world!" ){
    return 1;
    }else{
    return 0;
    }
}

My C# code :

[DllImport("test_match.dll",
CallingConvention = CallingConvention.Cdecl , 
CharSet = CharSet.Unicode)]
private static extern int check_string(string string_from_csharp)

My C# code of usage ( WPF ) :

int get_match_state = check_string(inputtext.Text);

MessageBox in C++ , says ... input is "hello world!"

But it always returns 0

Also , I tried to convert them to wchar_t , std::string with find() but nothing changed.

Where do I make a mistake? Thanks

  • 7
    You can not compare `const char*` with `==`. – tkausl Apr 30 '18 at 16:04
  • 5
    I'm not a C++ programmer, but it looks like you're comparing *pointers* at the moment. If your parameter were a C++ `string` I'd expect `==` to behave in a more "content-based" way. – Jon Skeet Apr 30 '18 at 16:04
  • CharSet.Unicode is wrong, you need CharSet.Ansi to match a char* argument. And you need to compare strings correctly, in the C language you use strcmp(). At least the CharSet mismatch should have been easy to discover with a debugger, be sure you know how to debug native code when pinvoked from C#. – Hans Passant Apr 30 '18 at 16:15

3 Answers3

2

You can't compare strings like that :

if (string_from_csharp == "hello world!" )

If you absolutely need to use char*, use strcmp, or strncmp.

extern "C" __declspec(dllexport) int check_string(const char* string_from_csharp);
bool check_string(const char* string_from_csharp)
{
  return (strcmp(string_from_csharp, "hello world!") == 0);
}

You may want to use std::string since you're in C++. In that case, you'd use std::string::compare.

MartinVeronneau
  • 1,296
  • 7
  • 24
  • 1
    thanks , but unfortunately your code not works , it always returns 0 , if I enter "hello world!" it returns false , If I enter "anything else" it returns false ... did you test your code ? with c# ? – Samuel Hardson Apr 30 '18 at 16:43
  • @SamuelHardson: did you change the CharSet as Hans Passant told you to do? – PaulF Apr 30 '18 at 16:50
  • @PaulF yes I did , I marked this answer and I added another answer of Hans Passant . thanks – Samuel Hardson Apr 30 '18 at 16:52
1

The correct answer belongs to Martin Véronneau and Hans Passant ( @hans-passant @martin-véronneau )

CharSet.Unicode is wrong, you need CharSet.Ansi to match a char* argument. And you need to compare strings correctly, in the C language you use strcmp(). At least the CharSet mismatch should have been easy to discover with a debugger, be sure you know how to debug native code when pinvoked from C#. – Hans Passant

Thank you Hans and Martin! The problem was CharSet = CharSet.Unicode, I changed to CharSet = CharSet.Ansi and now everything works fine!

0

As tkausl and Daisy have mentioned in the comments I believe in C++ you are comparing pointer values instead of the actual string value. In your case I think the easiest way to do the comparison would be to use strcmp to compare the 2 strings.

Noémie Lord
  • 751
  • 4
  • 22