what is happening here

G

Guest

In a .Net application, given a C++ dll with methods called by a C# GUI

Assume (x&y) is FALS

the following code in the C++ dll, when called from the C# interface

bool theMethod(unsigned int x)

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y
return true
els
return false


1. Does NOT print the text "True
2. Does return TRU

While the following code

theMethod(unsigned int x

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y

printf("")
return true

els
return false


1. Does NOT print the text "True
2. Does return FALS
 
P

pesso

I don't think printf done in a DLL actually prints out anything.
Write to a file instead.
 
G

Guest

Am I the only person who has ever seen this problem? How can any code written in this environment be trusted, when false returns true except when there is a stupid printf inserted in the code?
 
D

Dilip Krishnan

Honus,
This is not C++ in C# x&y is NOT a bolean.. but simply a number
which is a result of an & operation. And FYI the if state should read

if((x&y) > 0) {
printf("True");
}
 
D

Dilip Krishnan

My appologies for not undestanding your question... I see you want to
call a C++ dll.. ok.. theres a problem there too... unsigned int is not
CLS compliant.... so the code is not usable across languages. Now that
is the real problem why you are having eratic behaviour
HTH
 
G

Guest

Thanks for the response..
Are you saying Microsoft .Net C++ is not ansi-compliant
In that the method in question is written in C++, the construct of the if statement <"if (x&y)" vice if <"(x&y) > 0")> is correct, since in C++ a non-zero integer value is TRUE
The method is fully compliant with ansi C++, and returns a boolean to the C# method caller which is calculated based upon a fully C++ compliant value test. There is no cross-language usage of the unsigned int value.
Even if there was a syntactic problem with the code (which I believe there is not), that still does not explain why false is equal to true, unless there is a printf inserted in the code.
 
M

mikeb

Honus said:
In a .Net application, given a C++ dll with methods called by a C# GUI:

Assume (x&y) is FALSE

the following code in the C++ dll, when called from the C# interface:

bool theMethod(unsigned int x)
{
unsigned int y = 0x2 ;
if (x&y) // FALSE !
printf("True") ;
if (x&y)
return true ;
else
return false ;
}

1. Does NOT print the text "True"
2. Does return TRUE

While the following code:

theMethod(unsigned int x)
{
unsigned int y = 0x2 ;
if (x&y) // FALSE!
printf("True") ;
if (x&y)
{
printf("") ;
return true ;
}
else
return false ;
}

1. Does NOT print the text "True"
2. Does return FALSE

You aren't giving us enough information. How is the C# program calling
this method? What's the P/Invoke signature you're using? What is the
calling convention used by the C++ DLL?
 
D

Daniel O'Connell [C# MVP]

Dilip Krishnan said:
My appologies for not undestanding your question... I see you want to call
a C++ dll.. ok.. theres a problem there too... unsigned int is not CLS
compliant.... so the code is not usable across languages. Now that is the
real problem why you are having eratic behaviour
HTH
Thats not true. A type being non-CLS complaint doesn't make it unusable
across languages, it just means there is no requirement that all languages
support it. C++ and C# will work ine, VB.NET will as well in the next
iteration.
 
D

Daniel O'Connell [C# MVP]

Honus said:
Thanks for the response...
Are you saying Microsoft .Net C++ is not ansi-compliant?

Not entirely, the vs2003 C++ compiler is supposed to be compliant in the
upper 90th percentile(I forget what exactly) but it isn't exact. Due to the
complexity of C++ I doubt you are going to find any compiler with 100%
compliance.
In that the method in question is written in C++, the construct of the if
statement <"if (x&y)" vice if <"(x&y) > 0")> is correct, since in C++ a
non-zero integer value is TRUE.
The method is fully compliant with ansi C++, and returns a boolean to the
C# method caller which is calculated based upon a fully C++ compliant
value test. There is no cross-language usage of the unsigned int value.
Even if there was a syntactic problem with the code (which I believe there
is not), that still does not explain why false is equal to true, unless
there is a printf inserted in the code.

What is more important is the C++ dll a managed assembly or is it a native
dll? Answering mikeb's post will probably help as well
 
G

Guest

Well, this isn't an obscure subfeature of the language, method return values would seem a pretty fundamental part to me


----- Daniel O'Connell [C# MVP] wrote: ----


Honus said:
Thanks for the response..
Are you saying Microsoft .Net C++ is not ansi-compliant

Not entirely, the vs2003 C++ compiler is supposed to be compliant in the
upper 90th percentile(I forget what exactly) but it isn't exact. Due to the
complexity of C++ I doubt you are going to find any compiler with 100%
compliance
In that the method in question is written in C++, the construct of the if
statement <"if (x&y)" vice if <"(x&y) > 0")> is correct, since in C++ a
non-zero integer value is TRUE
The method is fully compliant with ansi C++, and returns a boolean to the
C# method caller which is calculated based upon a fully C++ compliant
value test. There is no cross-language usage of the unsigned int value
Even if there was a syntactic problem with the code (which I believe there
is not), that still does not explain why false is equal to true, unless
there is a printf inserted in the code

What is more important is the C++ dll a managed assembly or is it a native
dll? Answering mikeb's post will probably help as well
 
G

Guest

Howdy

The C# method is calling via the [DllImport] syntax

[DllImport "theDll.dll"
public static extern bool theMethod(int i)

......

int y = 0x2

bool isItTrue = theMethod(y)

....


Whatever calling convention, shoudln't TRUE always be TRUE and FALSE always be FALSE

----- mikeb wrote: ----

Honus wrote
In a .Net application, given a C++ dll with methods called by a C# GUI

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y
return true
els
return false


unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y

printf("")
return true

els
return false

2. Does return FALS

You aren't giving us enough information. How is the C# program calling
this method? What's the P/Invoke signature you're using? What is the
calling convention used by the C++ DLL
 
D

Daniel O'Connell [C# MVP]

Honus said:
Howdy:

The C# method is calling via the [DllImport] syntax:

[DllImport "theDll.dll"]
public static extern bool theMethod(int i) ;
just for kicks, change that to theMethod(uint i);
 
D

Daniel O'Connell [C# MVP]

Honus said:
In a .Net application, given a C++ dll with methods called by a C# GUI:

Assume (x&y) is FALSE

the following code in the C++ dll, when called from the C# interface:

bool theMethod(unsigned int x)
{
unsigned int y = 0x2 ;
if (x&y) // FALSE !
printf("True") ;
if (x&y)
return true ;
else
return false ;
}

1. Does NOT print the text "True"
2. Does return TRUE

While the following code:

theMethod(unsigned int x)
{
unsigned int y = 0x2 ;
if (x&y) // FALSE!
printf("True") ;
if (x&y)
{
printf("") ;
return true ;
}
else
return false ;
}

1. Does NOT print the text "True"
2. Does return FALSE
What happens if the same code is called via C++, what happens? Are you sure
this is a C# issue?
 
M

mikeb

Honus said:
Howdy:

The C# method is calling via the [DllImport] syntax:

[DllImport "theDll.dll"]
public static extern bool theMethod(int i) ;


......

int y = 0x2 ;

I assume this is a typo - if you're passing on 0x02 to 'theMethod()',
then the result of (x&y) is true, not false.
bool isItTrue = theMethod(y) ;

....


Whatever calling convention, shoudln't TRUE always be TRUE and FALSE always be FALSE?

I wrote a small test DLL and C# console app to test this, and got no
surprises:

====== test.pinvoke.cs =================================
using System;
using System.Runtime.InteropServices;


public class TestPInvoke {
[DllImport( "theDll.dll")]
public static extern bool theMethod( int i);

[DllImport( "theDll.dll")]
public static extern bool theMethod2( int i);

public static void Main() {
int y = 0x01;

Console.WriteLine( @"Calling ""theMethod( y)""");
bool res1 = theMethod( y);
Console.WriteLine();
Console.WriteLine();

Console.WriteLine( @"Calling ""theMethod2( y)""");
bool res2 = theMethod2( y);
Console.WriteLine();
Console.WriteLine();

Console.WriteLine( "theMethod( y) returned {0}", res1);
Console.WriteLine( "theMethod2( y) returned {0}", res2);

int z = 0x02;

Console.WriteLine( @"Calling ""theMethod( z)""");
res1 = theMethod( z);
Console.WriteLine();
Console.WriteLine();

Console.WriteLine( @"Calling ""theMethod2( z)""");
res2 = theMethod2( z);
Console.WriteLine();
Console.WriteLine();

Console.WriteLine( "theMethod( z) returned {0}", res1);
Console.WriteLine( "theMethod2( z) returned {0}", res2);
return;
}
}
==========================================================


====== theDll.cpp ========================================
#include <stdio.h>

extern "C" {
__declspec( dllexport)
bool theMethod(unsigned int x)
{
unsigned int y = 0x2 ;
if (x&y) // FALSE !
printf("True") ;
if (x&y)
return true ;
else
return false ;
}

__declspec( dllexport)
bool theMethod2(unsigned int x)
{
unsigned int y = 0x2 ;
if (x&y) // FALSE!
printf("True") ;
if (x&y)
{
printf("") ;
return true ;
}
else
return false ;
}
}
==========================================================

I compiled these with the following commands:

csc /t:exe test.pinvoke.cs
cl /LD /MD thedll.cpp

And ran "test.pinvoke.exe" with the expected results.

What's different between this code and your code?
 
G

Guest

Sorry, that's a typo here. The proper type is being passed (uint)

----- Daniel O'Connell [C# MVP] wrote: ----


Honus said:
Howdy
The C# method is calling via the [DllImport] syntax
[DllImport "theDll.dll"
public static extern bool theMethod(int i)
just for kicks, change that to theMethod(uint i)
 
G

Guest

Two diffrences

1. I did not declare the dll methods extern
2. I did not have this statement in the methods
__declspec( dllexport

I will add them and see what happens

Thanks for the posts

----- mikeb wrote: ----

Honus wrote
Howdy
The C# method is calling via the [DllImport] syntax
[DllImport "theDll.dll"
public static extern bool theMethod(int i)
int y = 0x2

I assume this is a typo - if you're passing on 0x02 to 'theMethod()',
then the result of (x&y) is true, not false

I wrote a small test DLL and C# console app to test this, and got no
surprises

====== test.pinvoke.cs ================================
using System
using System.Runtime.InteropServices


public class TestPInvoke
[DllImport( "theDll.dll")
public static extern bool theMethod( int i)

[DllImport( "theDll.dll")
public static extern bool theMethod2( int i)

public static void Main()
int y = 0x01

Console.WriteLine( @"Calling ""theMethod( y)""")
bool res1 = theMethod( y)
Console.WriteLine()
Console.WriteLine()

Console.WriteLine( @"Calling ""theMethod2( y)""")
bool res2 = theMethod2( y)
Console.WriteLine()
Console.WriteLine()

Console.WriteLine( "theMethod( y) returned {0}", res1)
Console.WriteLine( "theMethod2( y) returned {0}", res2)

int z = 0x02

Console.WriteLine( @"Calling ""theMethod( z)""")
res1 = theMethod( z)
Console.WriteLine()
Console.WriteLine()

Console.WriteLine( @"Calling ""theMethod2( z)""")
res2 = theMethod2( z)
Console.WriteLine()
Console.WriteLine()

Console.WriteLine( "theMethod( z) returned {0}", res1)
Console.WriteLine( "theMethod2( z) returned {0}", res2)
return


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


====== theDll.cpp =======================================
#include <stdio.h

extern "C"
__declspec( dllexport
bool theMethod(unsigned int x

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y
return true
els
return false


__declspec( dllexport
bool theMethod2(unsigned int x

unsigned int y = 0x2
if (x&y) // FALSE
printf("True")
if (x&y

printf("")
return true

els
return false


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

I compiled these with the following commands

csc /t:exe test.pinvoke.c
cl /LD /MD thedll.cp

And ran "test.pinvoke.exe" with the expected results

What's different between this code and your code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top