非托管C ++ Dll / C#bool返回问题 [英] Unmanaged C++ Dll / C# bool return problem

查看:74
本文介绍了非托管C ++ Dll / C#bool返回问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习使用C#中非托管C ++ DLL的基础知识。

今天早上我以为我到了某个地方,成功回来了

对C ++的正确答案 int SumArray(int ray [],int count)"


现在我遇到了C ++问题return(false)在C#中是真的。


这是C#代码。

=================== ======

使用System;

使用System.Runtime.InteropServices; //

命名空间GUI_03

{


class Class1

{

[DllImport(" Test05.dll")]

public static extern bool IsEven(int num);


[DllImport(" Test05。 dll)]

public static extern bool retFalse(int num);


[STAThread]

static void Main(string [] args)

{

for(int i = 0; i< 5; i ++)

{

Console.WriteLine(

" IsEven({0})= {1} \t retFalse({0})= {2}",

i, IsEven(i),retFalse(i));

}


Console.ReadLine();

}

}

}


======================= ==

接下来,最初的C ++代码。


//起点:

// http://www.codeproject.com/csharp/UseCDLLlibinCS.asp


#include< stdio.h>


extern" C"

{

__declspec(dllexport)bool IsEven(int somenum)

{

if(somenum ==(somenum / 2 * 2))

return(true);

else

返回(假);

}


__declspec(dllexport)bool retFalse(int somenum)

{

if(somenum ==(somenum / 2 * 2))

return(true);

else

return(false) ;

}

}


=================== ======

这是输出。


IsEven(0)= True retFalse(0)= True

IsEven(1)= False retFalse(1)= False

IsEven(2)= True retFalse(2)= True

IsEven(3)= False retFalse(3 )= False

IsEven(4)= True retFalse(4)= True


bothreturn(true);和return(false);正在工作。


=========================

这是注释掉retFalse()中的所有内容后的代码除了

" return(false);"


//起点:

// http://www.codeproject.com/csharp/UseCDLLlibinCS .asp


#include< stdio.h>


extern" C"

{

__declspec(dllexport)bool IsEven(int somenum)

{

if(somenum ==(somenum / 2 * 2))

返回(true);

其他

返回(false);

}


__declspec(dllexport)bool retFalse(int somenum)

{

/ * if(somenum ==(somenum / 2 * 2))

返回(true);

else * /

返回(false);

}

}


=========================

这是输出。


IsEven(0)= T. rue retFalse(0)= True

IsEven(1)= False retFalse(1)= True

IsEven(2)= True retFalse(2)= True
IsEven(3)= False retFalse(3)= True

IsEven(4)= True retFalse(4)= True


为什么是不是一直都错了?

=========================

这是另一个功能,显示我尝试的东西不起作用...


__declspec(dllexport)bool TorF(int num)

{

//返回(true);

//返回(false);

//返回(1 == 0);

// bool ret = false;

// return(ret);

// if(num ==(num / 2 * 2))/ / IsEven(num)

//返回(true);

返回(false);

//其他

// return(false);

//返回(true);

}

========= ================


有什么建议吗?


谢谢。


- 马克

解决方案

在星期二2005年3月29日14:38:06 -0500,Mark Jerde写道:

我正在尝试学习使用C#中非托管C ++ DLL的基础知识。
今天早上我以为我到了某个地方,成功地回到了正确的C ++答案。 int SumArray(int ray [],int count)"

现在我遇到了C ++问题return(false)在C#中是真的。




这方面有一个已知的错误。看看此消息是否适用于您的

问题:

http://groups-beta.google.com /group/...UTF-8&oe=UTF-8


-

Doug Harrison

Microsoft MVP - Visual C ++


Doug Harrison [MVP]写道:

现在我我遇到了C ++问题,返回(假)在C#中是真的。



这个领域有一个已知的错误。看看此消息是否适用于您的
问题:


http://groups-beta.google.com /group/...UTF-8&oe=UTF-8


哇,这太快了!谢谢!!


- Mark




" Mark Jerde" <毫安******** @ verizon.no.spam.net>在消息中写道

news:eq ************** @ TK2MSFTNGP12.phx.gbl ...

我正在尝试学习使用C#中非托管C ++ DLL的基础知识。
今天早上我以为我到了某个地方,成功地找回了正确的C ++答案 int SumArray(int ray [],int count)"

现在我遇到了C ++问题return(false)在C#中是真的。

这是C#代码。
========================= 使用System.Runtime.InteropServices; //

名称空间GUI_03
类Class1
{
[DllImport(" Test05.dll")]
public static extern bool IsEven(int num);

[DllImport(" Test05.dll")]
public static extern bool retFalse(int num);

[STAThread]
static void Main(string [] args)
{
for(int i = 0; i< 5; i ++)
{
控制台.WriteLine(
" IsEven({0})= {1} \t retFalse({0})= {2}",
i,IsEven(i),retFalse(i) );


Console.ReadLine();
}
}
}

====== ===================
接下来,最初的C ++代码。

//起点:
// < a rel =nofollowhref =http://www.codeproject.com/csharp/UseCDLLlibinCS.asptarget =_ blank> http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

#include< stdio.h>

extern" C"
{
__ declspec(dllexport)bool IsEven(int somenum)
{
if(somenum ==(somenum / 2 * 2))
返回(true);

返回(false);
}

__declspec (dllexport)bool retFalse(int somenum)
{
if(somenum ==(somenum / 2 * 2))
return(true);

返回(假);
}
}

=========================
这是输出。

IsEven(0)= True retFalse(0)= True
IsEven(1)= False retFalse(1)= False
IsEven(2) = True retFalse(2)= True
IsEven(3)= False retFalse(3)= False
IsEven(4)= True retFalse(4)= True

两者都是& ;返回(真);"和return(false);正在工作。

=========================
这是在retFalse中注释掉所有内容后的代码()除了
return(false);"

//起点:
// http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

#include< stdio。 h>

externC
{
__ declspec(dllexport)bool IsEven(int somenum)
{
if(somenum ==(somenum) / 2 * 2))
返回(true);

返回(false);
}

__ declspec(dllexport)bool retFalse(int somenum)
{
/ * if(somenum ==(somenum / 2 * 2))
return(true);
else * /
return(false) ;
}
}

=========================
这是输出。

IsEven(0)= True retFalse(0)= True
IsEven(1)= False retFalse(1)= True
IsEven(2)= True retFalse (2)=真的
IsEve n(3)= False retFalse(3)= True
IsEven(4)= True retFalse(4)= True

为什么rettalse总是假的?
=========================
这是另一个功能,显示我尝试过的东西不起作用......

__declspec(dllexport)bool TorF(int num)
//
// return(true);
// return(false);
// return(1 == 0);
// bool ret = false;
// return(ret);
// if(num ==(num / 2 * 2))// IsEven( num)
// return(true);
return(false);
// else
// return(false);
// return(true) ;
}
=========================

有什么建议吗?

谢谢。

- Mark




应该......

[返回:MarshalAs(UnmanagedType.I1)]

public static extern bool retFalse(int num);


查找MSDN文档,一个ansi C"布尔"是单字节值,true = 1,0 =

false,你应该在PInvoking时将它声明为I1返回!

C中的BOOL(在a中使用的typedef)大量的Windows API'是一个4字节

值,它正确地编组到bool。


Willy。


I''m trying to learn the very basics of using an unmanaged C++ DLL from C#.
This morning I thought I was getting somewhere, successfully getting back
the correct answers to a C++ " int SumArray(int ray[], int count)"

Now I''m having problems with C++ "return(false)" being True in C#.

Here is the C# code.
=========================
using System;
using System.Runtime.InteropServices; //
namespace GUI_03
{

class Class1
{
[DllImport("Test05.dll")]
public static extern bool IsEven(int num);

[DllImport("Test05.dll")]
public static extern bool retFalse(int num);

[STAThread]
static void Main(string[] args)
{
for (int i=0; i<5; i++)
{
Console.WriteLine(
"IsEven({0}) = {1} \t retFalse({0}) = {2}",
i, IsEven(i), retFalse(i));
}

Console.ReadLine();
}
}
}

=========================
Next, the initial C++ code.

// Starting point:
// http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

#include <stdio.h>

extern "C"
{
__declspec(dllexport) bool IsEven(int somenum)
{
if (somenum == (somenum / 2 * 2))
return(true);
else
return(false);
}

__declspec(dllexport) bool retFalse(int somenum)
{
if (somenum == (somenum / 2 * 2))
return(true);
else
return(false);
}
}

=========================
This is the output.

IsEven(0) = True retFalse(0) = True
IsEven(1) = False retFalse(1) = False
IsEven(2) = True retFalse(2) = True
IsEven(3) = False retFalse(3) = False
IsEven(4) = True retFalse(4) = True

Both "return(true);" and "return(false);" are working.

=========================
This is the code after commenting out everything in retFalse() except
"return(false);"

// Starting point:
// http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

#include <stdio.h>

extern "C"
{
__declspec(dllexport) bool IsEven(int somenum)
{
if (somenum == (somenum / 2 * 2))
return(true);
else
return(false);
}

__declspec(dllexport) bool retFalse(int somenum)
{
/*if (somenum == (somenum / 2 * 2))
return(true);
else */
return(false);
}
}

=========================
This is the output.

IsEven(0) = True retFalse(0) = True
IsEven(1) = False retFalse(1) = True
IsEven(2) = True retFalse(2) = True
IsEven(3) = False retFalse(3) = True
IsEven(4) = True retFalse(4) = True

Why isn''t retFalse always False??
=========================
Here is another function showing things I tried that didn''t work...

__declspec(dllexport) bool TorF(int num)
{
//return(true);
//return(false);
//return(1 == 0);
//bool ret = false;
//return(ret);
// if(num == (num / 2 * 2)) // IsEven(num)
//return(true);
return(false);
// else
//return(false);
// return(true);
}
=========================

Any suggestions?

Thanks.

-- Mark

解决方案

On Tue, 29 Mar 2005 14:38:06 -0500, Mark Jerde wrote:

I''m trying to learn the very basics of using an unmanaged C++ DLL from C#.
This morning I thought I was getting somewhere, successfully getting back
the correct answers to a C++ " int SumArray(int ray[], int count)"

Now I''m having problems with C++ "return(false)" being True in C#.



There''s a known bug in this area. See if this message applies to your
problem:

http://groups-beta.google.com/group/...UTF-8&oe=UTF-8

--
Doug Harrison
Microsoft MVP - Visual C++


Doug Harrison [MVP] wrote:

Now I''m having problems with C++ "return(false)" being True in C#.



There''s a known bug in this area. See if this message applies to your
problem:


http://groups-beta.google.com/group/...UTF-8&oe=UTF-8

Wow, that was fast! Thanks!!

-- Mark



"Mark Jerde" <ma********@verizon.no.spam.net> wrote in message
news:eq**************@TK2MSFTNGP12.phx.gbl...

I''m trying to learn the very basics of using an unmanaged C++ DLL from C#.
This morning I thought I was getting somewhere, successfully getting back
the correct answers to a C++ " int SumArray(int ray[], int count)"

Now I''m having problems with C++ "return(false)" being True in C#.

Here is the C# code.
=========================
using System;
using System.Runtime.InteropServices; //
namespace GUI_03
{

class Class1
{
[DllImport("Test05.dll")]
public static extern bool IsEven(int num);

[DllImport("Test05.dll")]
public static extern bool retFalse(int num);

[STAThread]
static void Main(string[] args)
{
for (int i=0; i<5; i++)
{
Console.WriteLine(
"IsEven({0}) = {1} \t retFalse({0}) = {2}",
i, IsEven(i), retFalse(i));
}

Console.ReadLine();
}
}
}

=========================
Next, the initial C++ code.

// Starting point:
// http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

#include <stdio.h>

extern "C"
{
__declspec(dllexport) bool IsEven(int somenum)
{
if (somenum == (somenum / 2 * 2))
return(true);
else
return(false);
}

__declspec(dllexport) bool retFalse(int somenum)
{
if (somenum == (somenum / 2 * 2))
return(true);
else
return(false);
}
}

=========================
This is the output.

IsEven(0) = True retFalse(0) = True
IsEven(1) = False retFalse(1) = False
IsEven(2) = True retFalse(2) = True
IsEven(3) = False retFalse(3) = False
IsEven(4) = True retFalse(4) = True

Both "return(true);" and "return(false);" are working.

=========================
This is the code after commenting out everything in retFalse() except
"return(false);"

// Starting point:
// http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

#include <stdio.h>

extern "C"
{
__declspec(dllexport) bool IsEven(int somenum)
{
if (somenum == (somenum / 2 * 2))
return(true);
else
return(false);
}

__declspec(dllexport) bool retFalse(int somenum)
{
/*if (somenum == (somenum / 2 * 2))
return(true);
else */
return(false);
}
}

=========================
This is the output.

IsEven(0) = True retFalse(0) = True
IsEven(1) = False retFalse(1) = True
IsEven(2) = True retFalse(2) = True
IsEven(3) = False retFalse(3) = True
IsEven(4) = True retFalse(4) = True

Why isn''t retFalse always False??
=========================
Here is another function showing things I tried that didn''t work...

__declspec(dllexport) bool TorF(int num)
{
//return(true);
//return(false);
//return(1 == 0);
//bool ret = false;
//return(ret);
// if(num == (num / 2 * 2)) // IsEven(num)
//return(true);
return(false);
// else
//return(false);
// return(true);
}
=========================

Any suggestions?

Thanks.

-- Mark



Should be...
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool retFalse(int num);

Lookup the MSDN docs, an ansi C "bool" is a single byte value, true = 1, 0 =
false, you should declare it as a I1 return when PInvoking !
A BOOL in C (typedef used in a great number of windows API''s) is a 4 byte
value wich is marshaled correctly to bool.

Willy.


这篇关于非托管C ++ Dll / C#bool返回问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆