473,791 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Straight to the IntPtr -

Hi,
I'm trying to convert the following line to c# from VB6

If hrRapiInit >= 0 Then 'SUCCEEDED

where hrRapiInit is declared as a Long.

In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
function i have imported, but obviously, the line

if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED

returns an error.

What is the equivalent c# test to see if an IntPtr is greater than or
equal to 'zero'? Is there such a test?

Many thanks in advance,
James Randle.

Jul 25 '06 #1
9 6189
try if(IntPtr.ToInt 32() 0)

Jul 25 '06 #2
PandaGoat,

Cheers for the info :o)
Pa*******@gmail .com wrote:
try if(IntPtr.ToInt 32() 0)
Jul 25 '06 #3
James,
Remember that IntPtr.ToInt32 is "valid" on Win32 platforms.

http://msdn2.microsoft.com/en-us/lib...r.toint32.aspx

For Win64 platforms its probably safer to use IntPtr.ToInt64 (as it sounds
like ToInt32 with throw an exception).

http://msdn2.microsoft.com/en-us/lib...r.toint64.aspx
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
| Hi,
| I'm trying to convert the following line to c# from VB6
|
| If hrRapiInit >= 0 Then 'SUCCEEDED
|
| where hrRapiInit is declared as a Long.
|
| In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
| function i have imported, but obviously, the line
|
| if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED
|
| returns an error.
|
| What is the equivalent c# test to see if an IntPtr is greater than or
| equal to 'zero'? Is there such a test?
|
| Many thanks in advance,
| James Randle.
|
Jul 25 '06 #4
Hi,

You should ALWAYS compare it to IntPtr.Zero . so instead you should do
if ( intPtr.Zero != hrRapiInit )

or
if ( IntPtr.Zero.Equ als( hrRapiInit )
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
Hi,
I'm trying to convert the following line to c# from VB6

If hrRapiInit >= 0 Then 'SUCCEEDED

where hrRapiInit is declared as a Long.

In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
function i have imported, but obviously, the line

if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED

returns an error.

What is the equivalent c# test to see if an IntPtr is greater than or
equal to 'zero'? Is there such a test?

Many thanks in advance,
James Randle.

Jul 25 '06 #5
Ignacio,

Thankyou for replying. In the context i am using it in, i am looking to
test >= 0, not just = or !=. Is there a way i can accomplish this using
these techniques?

I am using the function CeRapiInitEx(RA PIINIT ra), which uses the
structure

typedef struct _RAPIINIT {
DWORD cbSize;
HANDLE heRapiInit;
HRESULT hrRapiInit;
} RAPIINIT;

and on pinvoke.net, the supposed declaration is...

[StructLayout(La youtKind.Explic it)]
public struct RAPIINIT
{
[FieldOffset(0)] public int cbsize;
[FieldOffset(4)] public System.IntPtr heRapiInit;
[FieldOffset(8)] public System.IntPtr hrRapiInit;
};

As you can see, an IntPtr has been used for hrRapiInit, but the test in
the SDK says you must test for >=0.

Is the c# struct incorrect? Should i be passing an int as a ref instead
of the second IntPtr?

Cheers,
James Randle
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

You should ALWAYS compare it to IntPtr.Zero . so instead you should do
if ( intPtr.Zero != hrRapiInit )

or
if ( IntPtr.Zero.Equ als( hrRapiInit )
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
Hi,
I'm trying to convert the following line to c# from VB6

If hrRapiInit >= 0 Then 'SUCCEEDED

where hrRapiInit is declared as a Long.

In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
function i have imported, but obviously, the line

if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED

returns an error.

What is the equivalent c# test to see if an IntPtr is greater than or
equal to 'zero'? Is there such a test?

Many thanks in advance,
James Randle.
Jul 25 '06 #6
Jay,
Thans for taking the time to leave a comment.
I have added a reply (above), because i am unsure as to the accuracy of
the dllimport declaration i found for the structure RAPIINIT.

Many thanks,
James

Jay B. Harlow [MVP - Outlook] wrote:
James,
Remember that IntPtr.ToInt32 is "valid" on Win32 platforms.

http://msdn2.microsoft.com/en-us/lib...r.toint32.aspx

For Win64 platforms its probably safer to use IntPtr.ToInt64 (as it sounds
like ToInt32 with throw an exception).

http://msdn2.microsoft.com/en-us/lib...r.toint64.aspx
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
| Hi,
| I'm trying to convert the following line to c# from VB6
|
| If hrRapiInit >= 0 Then 'SUCCEEDED
|
| where hrRapiInit is declared as a Long.
|
| In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
| function i have imported, but obviously, the line
|
| if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED
|
| returns an error.
|
| What is the equivalent c# test to see if an IntPtr is greater than or
| equal to 'zero'? Is there such a test?
|
| Many thanks in advance,
| James Randle.
|
Jul 25 '06 #7
Jay,
Thanks for taking the time to leave a comment.
I have added a reply (above), because i am unsure as to the accuracy of
the dllimport declaration i found for the structure RAPIINIT.

Many thanks,
James

Jay B. Harlow [MVP - Outlook] wrote:
James,
Remember that IntPtr.ToInt32 is "valid" on Win32 platforms.

http://msdn2.microsoft.com/en-us/lib...r.toint32.aspx

For Win64 platforms its probably safer to use IntPtr.ToInt64 (as it sounds
like ToInt32 with throw an exception).

http://msdn2.microsoft.com/en-us/lib...r.toint64.aspx
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
| Hi,
| I'm trying to convert the following line to c# from VB6
|
| If hrRapiInit >= 0 Then 'SUCCEEDED
|
| where hrRapiInit is declared as a Long.
|
| In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
| function i have imported, but obviously, the line
|
| if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED
|
| returns an error.
|
| What is the equivalent c# test to see if an IntPtr is greater than or
| equal to 'zero'? Is there such a test?
|
| Many thanks in advance,
| James Randle.
|
Jul 25 '06 #8
Your structure is wrong. HRESULT is not a handle, it's an int. Try this:

[StructLayout(La youtKind.Sequen tial)]
public struct RAPIINIT
{
public int cbsize;
public System.IntPtr heRapiInit;
public int hrRapiInit;
};

and modify your CeRapiInitEx to take a ref to the above structure, i.e.
something like:
int CeRapiInitEx(re f RAPIINIT ra)

/claes

"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** ************@m7 9g2000cwm.googl egroups.com...
Ignacio,

Thankyou for replying. In the context i am using it in, i am looking to
test >= 0, not just = or !=. Is there a way i can accomplish this using
these techniques?

I am using the function CeRapiInitEx(RA PIINIT ra), which uses the
structure

typedef struct _RAPIINIT {
DWORD cbSize;
HANDLE heRapiInit;
HRESULT hrRapiInit;
} RAPIINIT;

and on pinvoke.net, the supposed declaration is...

[StructLayout(La youtKind.Explic it)]
public struct RAPIINIT
{
[FieldOffset(0)] public int cbsize;
[FieldOffset(4)] public System.IntPtr heRapiInit;
[FieldOffset(8)] public System.IntPtr hrRapiInit;
};

As you can see, an IntPtr has been used for hrRapiInit, but the test in
the SDK says you must test for >=0.

Is the c# struct incorrect? Should i be passing an int as a ref instead
of the second IntPtr?

Cheers,
James Randle
Ignacio Machin ( .NET/ C# MVP ) wrote:
>Hi,

You should ALWAYS compare it to IntPtr.Zero . so instead you should do
if ( intPtr.Zero != hrRapiInit )

or
if ( IntPtr.Zero.Equ als( hrRapiInit )
--
--
Ignacio Machin,
ignacio.mach in AT dot.state.fl.us
Florida Department Of Transportation

"pigeonrandl e" <pi**********@h otmail.comwrote in message
news:11******* **************@ p79g2000cwp.goo glegroups.com.. .
Hi,
I'm trying to convert the following line to c# from VB6

If hrRapiInit >= 0 Then 'SUCCEEDED

where hrRapiInit is declared as a Long.

In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
function i have imported, but obviously, the line

if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED

returns an error.

What is the equivalent c# test to see if an IntPtr is greater than or
equal to 'zero'? Is there such a test?

Many thanks in advance,
James Randle.

Jul 25 '06 #9
Claes,
Thankyou, you've confirmed what i suspected ...
I thought it was 'easy' just to look for struct and dllimport
declarations on the web, instead of creating them myself (and
understanding them in the process) from the SDK pages on MSDN. No more
laziness for me.

Thanks again,
James.
Claes Bergefall wrote:
Your structure is wrong. HRESULT is not a handle, it's an int. Try this:

[StructLayout(La youtKind.Sequen tial)]
public struct RAPIINIT
{
public int cbsize;
public System.IntPtr heRapiInit;
public int hrRapiInit;
};

and modify your CeRapiInitEx to take a ref to the above structure, i.e.
something like:
int CeRapiInitEx(re f RAPIINIT ra)

/claes

"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** ************@m7 9g2000cwm.googl egroups.com...
Ignacio,

Thankyou for replying. In the context i am using it in, i am looking to
test >= 0, not just = or !=. Is there a way i can accomplish this using
these techniques?

I am using the function CeRapiInitEx(RA PIINIT ra), which uses the
structure

typedef struct _RAPIINIT {
DWORD cbSize;
HANDLE heRapiInit;
HRESULT hrRapiInit;
} RAPIINIT;

and on pinvoke.net, the supposed declaration is...

[StructLayout(La youtKind.Explic it)]
public struct RAPIINIT
{
[FieldOffset(0)] public int cbsize;
[FieldOffset(4)] public System.IntPtr heRapiInit;
[FieldOffset(8)] public System.IntPtr hrRapiInit;
};

As you can see, an IntPtr has been used for hrRapiInit, but the test in
the SDK says you must test for >=0.

Is the c# struct incorrect? Should i be passing an int as a ref instead
of the second IntPtr?

Cheers,
James Randle
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

You should ALWAYS compare it to IntPtr.Zero . so instead you should do
if ( intPtr.Zero != hrRapiInit )

or
if ( IntPtr.Zero.Equ als( hrRapiInit )
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"pigeonrand le" <pi**********@h otmail.comwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
Hi,
I'm trying to convert the following line to c# from VB6

If hrRapiInit >= 0 Then 'SUCCEEDED

where hrRapiInit is declared as a Long.

In c#, hrRapiInit must be declared as an IntPtr to fit in with the dll
function i have imported, but obviously, the line

if (hrRapiInit >= IntPtr.Zero) //SUCCEEDED

returns an error.

What is the equivalent c# test to see if an IntPtr is greater than or
equal to 'zero'? Is there such a test?

Many thanks in advance,
James Randle.
Jul 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
31315
by: Nuno Esculcas | last post by:
Hello, I come from C++ and i now have to work with C#, and someone tell me that bye bye pointers but i think this is not true, i must convert a DIB image in something that i can use in C# (like Image or Bitmap) and the only thing i can do is a DIB into a IntPtr (a pointer). I do this with the platform invoke method of Win32: internal static extern int GdipCreateBitmapFromGdiDib( IntPtr bminfo,
13
7436
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a method to an IntPtr? / Christian
10
4658
by: Tamir Khason | last post by:
I have a pointer to array and I want to apply indexing to this Array so I have function (name it IntPrt Func) to go to certain member I can use (as C++) Func, but in C# I recieve an error "Cannot apply indexing with to an expression of type 'System.IntPtr'". How to get rid of it? TNX --
2
6871
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public static explicit System.IntPtr (long);
6
1566
by: active | last post by:
I downloaded the Win32 Library which saved me MUCH work. Some (All?) of the returned handles are typed int. As I used them I often changed them in my copy of win32 to IntPtr because an argument that used it was typed IntPtr. Now I wonder if a Handle make more sense being an integer or an IntPtr. I believe it's really an index rather than a pointer so Integer seems to
0
3956
by: Jørn Jensen | last post by:
Hey! I have an issue with creating bitmap from an IntPtr. The IntPtr is created from a Twain scanner, which seem to work perfectly in it's original state (the app is originally written by somebody else). It's when I add the I get an error. Extracts of my code and the error statement from the Visual Studio Debug bellow: Select Case cmd Case TwainCommand.CloseRequest EndingScan()
7
7848
by: justin.kruger | last post by:
I am having a problem with exchanging handles with c# and the "CP210xManufacturing.dll" in a device wrapper that i am working on. I have created a wrapper class to address some of the functions in the dll, but not all of them yet. To my knowledge they should be almost correct, however this is my first try at exchanging handles between legacy code and a managed enviroment. It seems that I am able to get a handle from "CP210x_Open,"...
1
4211
by: dcurington | last post by:
I have to call a function. This is from the SDK: INT is_GetActSeqBuf (HIDS hf, INT* pnNum, char** ppcMem, char** ppcMemLast); I have demo C# code that works. Here is the prototype: private static extern int is_GetActSeqBuf (int hCam, ref int pnNum, ref IntPtr ppcMem, ref IntPtr ppcMemLast); And this is the way it is used in the C# code. IntPtr pMem = new IntPtr(); IntPtr pLast = new IntPtr();
0
2137
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public Detail(int _age)
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.