Straight to the IntPtr -

  • Thread starter Thread starter pigeonrandle
  • Start date Start date
P

pigeonrandle

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.
 
James,
Remember that IntPtr.ToInt32 is "valid" on Win32 platforms.

http://msdn2.microsoft.com/en-us/library/system.intptr.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/library/system.intptr.toint64.aspx


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| 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.
|
 
Hi,

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

or
if ( IntPtr.Zero.Equals( hrRapiInit )
 
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(RAPIINIT ra), which uses the
structure

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

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

[StructLayout(LayoutKind.Explicit)]
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
 
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,
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
 
Your structure is wrong. HRESULT is not a handle, it's an int. Try this:

[StructLayout(LayoutKind.Sequential)]
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(ref RAPIINIT ra)

/claes

pigeonrandle said:
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(RAPIINIT ra), which uses the
structure

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

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

[StructLayout(LayoutKind.Explicit)]
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

Hi,

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

or
if ( IntPtr.Zero.Equals( hrRapiInit )


--
 
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 said:
Your structure is wrong. HRESULT is not a handle, it's an int. Try this:

[StructLayout(LayoutKind.Sequential)]
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(ref RAPIINIT ra)

/claes

pigeonrandle said:
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(RAPIINIT ra), which uses the
structure

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

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

[StructLayout(LayoutKind.Explicit)]
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

Hi,

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

or
if ( IntPtr.Zero.Equals( hrRapiInit )


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



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.
 

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

Back
Top