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. | | | | re: Straight to the IntPtr -
try if(IntPtr.ToInt32() 0) | | | | re: Straight to the IntPtr -
PandaGoat,
Cheers for the info :o) PandaGoat@gmail.com wrote: Quote:
try if(IntPtr.ToInt32() 0)
| | | | re: Straight to the IntPtr -
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
"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
news:1153825570.440315.68770@p79g2000cwp.googlegro ups.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.
| | | | | re: Straight to the IntPtr -
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
"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
news:1153825570.440315.68770@p79g2000cwp.googlegro ups.com... Quote:
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.
>
| | | | re: Straight to the IntPtr -
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
Ignacio Machin ( .NET/ C# MVP ) wrote: Quote:
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
>
>
>
"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
news:1153825570.440315.68770@p79g2000cwp.googlegro ups.com... Quote:
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.
| | | | re: Straight to the IntPtr -
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: Quote:
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
>
>
"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
news:1153825570.440315.68770@p79g2000cwp.googlegro ups.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.
|
| | | | re: Straight to the IntPtr -
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: Quote:
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
>
>
"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
news:1153825570.440315.68770@p79g2000cwp.googlegro ups.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.
|
| | | | re: Straight to the IntPtr -
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" <pigeonrandle@hotmail.comwrote in message
news:1153833994.839000.4550@m79g2000cwm.googlegrou ps.com... Quote:
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
>
>
Ignacio Machin ( .NET/ C# MVP ) wrote: Quote:
>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
>>
>>
>>
>"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
>news:1153825570.440315.68770@p79g2000cwp.googlegr oups.com... Quote:
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.
>
>
| | | | re: Straight to the IntPtr -
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: Quote:
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" <pigeonrandle@hotmail.comwrote in message
news:1153833994.839000.4550@m79g2000cwm.googlegrou ps.com... Quote:
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
Ignacio Machin ( .NET/ C# MVP ) wrote: Quote:
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
>
>
>
"pigeonrandle" <pigeonrandle@hotmail.comwrote in message
news:1153825570.440315.68770@p79g2000cwp.googlegro ups.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.
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|