C# DllImport with C++ boolean function not returning correctly
I found the solution for your problem. Your declaration should be preceded with this marshaling:[return:MarshalAs(UnmanagedType.I1)]
so everything should look like this:
[DllImport("Whisper.dll", EntryPoint="Exist", CallingConvention=CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool Exist([MarshalAs(UnmanagedType.LPStr)] string name);
I tested it in my very simple example and it worked!
EDIT
Why this happens? C defines bool as 4 bytes int (as some of you have said) and C++ defines it as 1 byte. C# team decided to use 4 byte bool as default during PInvoke because most of the system API function use 4 bytes values as bool. If you want to change this behavior you have to do it with marshaling specifying that you want to use 1 byte value.
Why does my dllimport function always return true?
bool
is a horrible native type. Everyone does it their own way. In C#, the default interop on bool
maps to the BOOL
C++ type, which deals with different values than your bool
.
You need to use [return:MarshalAs(UnmanagedType.I1)]
to specify the correct marshalling, and don't forget to use the C calling convention.
C++ from C#: C++ function (in a DLL) returning false, but C# thinks it's true!
Try [return: MarshalAs (UnmanagedType.I1)]
. By default, C# interop marshals C# bool
as the Win32 BOOL
, which is the same as int
, while C++ bool
is one byte AFAIR. Thus, C#'s default marshaling expects the return value to be a BOOL
in the eax
register, and picks up some non-zero garbage because C++ bool
is returned in al
.
C# calling C++ DLL returns incorrect value
A solution to your problem can be to precede your declaration withe the following marshaling.
[return:MarshalAs(UnmanagedType.I1)]
This may happen because C defines bool as 4 bytes int and C++ defines it as 1 byte. C# team decided to use 4 byte bool as default during PInvoke because most of the system API function use 4 bytes values as bool. If you want to change this behavior you have to do it with marshaling specifying that you want to use 1 byte value. Also another solution can be to try changing the return type from bool to int, that should also fix the issue.
For more information, have a look at the answer mentioned here:
C# DllImport with C++ boolean function not returning correctly
boolean function returning false even though condition is satisfied to return true in c++. Why?
You should use test_length - 1
instead of test_lenght + 1
, because in the new reversed string you have some extra characters which you can't see if you print them.
The .length()
function returns you exactly the number of characters in the string. So you either go with test_length
, but you do i>0
, or if you go in the loop with i>=0
you go with test_length - 1
, so you will be sure that there are no blank characters at the end of new string.
But, if you start with test_length
, you will need to edit, since there is no character at test_length
position:
test_s_reverse += test_s[i-1];
If you use pure C++ code, it should look like this:
bool is_palindrome(int test){
string test_s = to_string(test);
int test_length = test_s.length();
string test_s_reverse;
for (int i=(test_length); i>0; i--){
test_s_reverse += test_s[i-1];
}
if (test_s_reverse == test_s){
return true;
}
else {
return false;
}
}
Recursive C++ function not returning correct boolean value
You need to return the result of find
return find(v, value, n - 1);
in your function.
If you turn on warnings, the compiler will tell you you're doing something wrong.
Also, your base case seems incorrect. 0
is a valid index. You should stop if n
is -1
.
Related to your question,using a recursive approach to find an element in a contiguous container seems odd. Why don't you just try something like
std::find(v.begin(), v.begin() + n, value);
You can compare the result of find
to v.begin() + n
to check if the element is found.
Related Topics
Entity Framework the Underlying Provider Failed on Open
How to Dllexport a C++ Class for Use in a C# Application
Insert Text into the Textbox of Another Application
Perform Button Click Event When User Press Enter Key in Textbox
How to Install Python for .Net on Windows
Why Does "Dtoa.C" Contain So Much Code
Which Linux Distribution Is Best for Developing a Mono Application in a Virtual Machine
Interop.Word Documents.Open Is Null
How to Develop iOS App Using Xamarin Studio on Windows
How Does Garbage Collection and Scoping Work in C#
Incorrect Syntax Near the Keyword 'User'
How to Hook into Hardware Keyboard Events at an Application Level
Detect Browser Close on Asp.Net
Getting Value from HTML Radio Button - in Aspx-C#
When Do You Use Extension Methods, Ext. Methods VS. Inheritance