473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Marshal.GetLastWin32Error()

Hi there,

Does anyone know if this (example) is safe:

[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowRect(IntPtr hWnd, ref RECT
rect);

int rc = GetWindowRect(hWnd, ref rect);
if (rc == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Error");
}

My concern is the call to "Marshal.GetLastWin32Error()". Can the value
returned by this function be modified by the call to the exception
constructor itself. For instance, could construction of the "Error" string
overwrite "Marshal.GetLastWin32Error()" on failure given that it might also
call into the WinAPI. This would be a problem in C++ for instance where you
can't depend on the order of the parameters being passed (i.e., you can't
reliably pass "GetLastError()" as the first arg since the 2nd.arg might be
evaluated first - on failure it would then overwrite the first arg)
May 8 '07 #1
4 8791
Larry,

The only way that GetLastWin32Error is going to return a different value
is if you use another call to unmanaged code. You should store the error if
you are going to perform an operation which could possibly change it, and
you need it for later.

I am curious, why are you passing an error string or the last error to
the Win32Exception class? If you use the default constructor, it should get
the value of the last win 32 error and also create the appropriate exception
message.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Larry Smith" <no_spam@_nospam.comwrote in message
news:eJ**************@TK2MSFTNGP02.phx.gbl...
Hi there,

Does anyone know if this (example) is safe:

[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowRect(IntPtr hWnd, ref RECT
rect);

int rc = GetWindowRect(hWnd, ref rect);
if (rc == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Error");
}

My concern is the call to "Marshal.GetLastWin32Error()". Can the value
returned by this function be modified by the call to the exception
constructor itself. For instance, could construction of the "Error" string
overwrite "Marshal.GetLastWin32Error()" on failure given that it might
also call into the WinAPI. This would be a problem in C++ for instance
where you can't depend on the order of the parameters being passed (i.e.,
you can't reliably pass "GetLastError()" as the first arg since the
2nd.arg might be evaluated first - on failure it would then overwrite the
first arg)

May 8 '07 #2
Thanks for the feedback:
The only way that GetLastWin32Error is going to return a different value
is if you use another call to unmanaged code. You should store the error
if you are going to perform an operation which could possibly change it,
and you need it for later.
I won't be calling unmanaged code directly. My concern are calls that .NET
itself makes. Those calls will invoke "SetLastError()" in the WinAPI but
does this affect "Marshal.GetLastWin32Error()" for clients (i.e., me). Note
that I can always save the result before throwing the exception (passing
this value to the constructor) but I'd like to know what the rules are.
I am curious, why are you passing an error string or the last error to the
Win32Exception class? If you use the default constructor, it should get
the value of the last win 32 error and also create the appropriate
exception message.
I'm still experimenting but I want to capture context info at the time of
call (the parameters that caused the failure for instance). Take this as a
possible solution for example (which I'm still considering):

class Win32LastError : Win32Exception
{
public Win32LastError(string messageEx)
{
m_MessageEx = messageEx;
}

public string MessageEx
{
get
{
return m_MessageEx;
}
}

private string m_MessageEx;
}

When the base class' default constructor is called, could the last error
have already been overwritten during construction of the "messageEx"
parameter itself (in the above constructor). It relies on the WinAPI behind
the scenes so it could potentially change the last error before the base
class constructor even starts (or so I'm speculating). This would be
particularly true if a non-trivial expression is used to create the string
in the first place (at the point of call). Note that this won't involve
unmanaged code however.
May 8 '07 #3
Larry,

The GetLastWin32Error method will only return the value of GetLastError
for P/Invoke calls, not for CLR internal calls (as the documentation
states). Because of this, you only have to worry about the code being
changed if another call on the same thread makes another call through the
P/Invoke layer.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Larry Smith" <no_spam@_nospam.comwrote in message
news:eR****************@TK2MSFTNGP03.phx.gbl...
Thanks for the feedback:
>The only way that GetLastWin32Error is going to return a different value
is if you use another call to unmanaged code. You should store the error
if you are going to perform an operation which could possibly change it,
and you need it for later.

I won't be calling unmanaged code directly. My concern are calls that .NET
itself makes. Those calls will invoke "SetLastError()" in the WinAPI but
does this affect "Marshal.GetLastWin32Error()" for clients (i.e., me).
Note that I can always save the result before throwing the exception
(passing this value to the constructor) but I'd like to know what the
rules are.
>I am curious, why are you passing an error string or the last error to
the Win32Exception class? If you use the default constructor, it should
get the value of the last win 32 error and also create the appropriate
exception message.

I'm still experimenting but I want to capture context info at the time of
call (the parameters that caused the failure for instance). Take this as a
possible solution for example (which I'm still considering):

class Win32LastError : Win32Exception
{
public Win32LastError(string messageEx)
{
m_MessageEx = messageEx;
}

public string MessageEx
{
get
{
return m_MessageEx;
}
}

private string m_MessageEx;
}

When the base class' default constructor is called, could the last error
have already been overwritten during construction of the "messageEx"
parameter itself (in the above constructor). It relies on the WinAPI
behind the scenes so it could potentially change the last error before the
base class constructor even starts (or so I'm speculating). This would be
particularly true if a non-trivial expression is used to create the string
in the first place (at the point of call). Note that this won't involve
unmanaged code however.

May 8 '07 #4
The GetLastWin32Error method will only return the value of GetLastError
for P/Invoke calls, not for CLR internal calls (as the documentation
states). Because of this, you only have to worry about the code being
changed if another call on the same thread makes another call through the
P/Invoke layer.
I just assumed as much and the docs basically say so but I didn't want to
get tripped up with subtle problems later (since I'm relatively new to
..NET). Thanks very much for your help.
May 8 '07 #5

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

Similar topics

13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
6
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. ...
2
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This...
10
by: active | last post by:
GetLastWin32Error exposes the Win32 GetLastError API method from Kernel32.DLL. This method exists because it is not safe to make a direct platform invoke call to GetLastError to obtain this...
5
by: Anurag | last post by:
I have been chasing a problem in my code since hours and it bolis down to this import marshal marshal.dumps(str(123)) != marshal.dumps(str("123")) Can someone please tell me why? when...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
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...
0
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.