473,394 Members | 1,875 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,394 software developers and data experts.

PInvoke GlobalGetAtomName

PLS
I'm having problem calling the Win32 function GlobalGetAtomName from C#.
I'm getting a debugger popup telling me that the called function has
unbalanced the stack.

I'm hoping someone here can see what I'm doing wrong.

Here is the function declaration, in a class call DDEFunctions:

[DllImport("kernel32.dll", SetLastError=true,
CharSet=CharSet.Unicode,
CallingConvention = CallingConvention.StdCall,
EntryPoint = "GlobalGetAtomNameW")]
public static extern int GlobalGetAtomName(ushort nAtom,
out String lpBuffer, int nSize);

And here is the code that calls it

public static String GetAtomString(ushort Atom)
{
if (Atom == 0)
return null;

int BufferLen;
String Buffer;

BufferLen = 514;
Buffer = new String('\0', BufferLen);

BufferLen = DDEFunctions.GlobalGetAtomName
( Atom, out Buffer, BufferLen);

if (BufferLen == 0)
{
System.Diagnostics.Trace.WriteLine("GetAtomString failed
with DLL error number " + Marshal.GetLastWin32Error().ToString());
return null;
}
else
{
Buffer = Buffer.Substring(1, BufferLen);
return Buffer;
}
}
Does anyone see the problem?

Thanks very much,
++PLS
Nov 4 '07 #1
4 2946
PLS
It was out, but changing it to ref did not change anything.

The definition is now
[DllImport("kernel32.dll", SetLastError=true,
CharSet=CharSet.Unicode,
CallingConvention = CallingConvention.StdCall,
EntryPoint = "GlobalGetAtomNameW")]
public static extern int GlobalGetAtomName(ushort nAtom,
ref StringBuilder lpBuffer, long nSize);
In article <EE**********************************@microsoft.co m>,
mv*@spam.guard.caspershouse.com says...
What is the declaration of the function with the StringBuilder? It
shouldn't be "out", as the P/Invoke layer will know that the buffer has to
be marshaled in and out when the StringBuilder is in place.

Nov 4 '07 #2
On 2007-11-03 19:34:28 -0700, PLS <no****@nowhere.comsaid:
It was out, but changing it to ref did not change anything.
I think he means to make the parameter not "out" _or_ "ref". Just make
it a regular by-value parameter. P/invoke will handle the rest.

Pete

Nov 4 '07 #3
"PLS" <no****@nowhere.comwrote in message
news:MP************************@msnews.microsoft.c om...
It was out, but changing it to ref did not change anything.

The definition is now
[DllImport("kernel32.dll", SetLastError=true,
CharSet=CharSet.Unicode,
CallingConvention = CallingConvention.StdCall,
EntryPoint = "GlobalGetAtomNameW")]
public static extern int GlobalGetAtomName(ushort nAtom,
ref StringBuilder lpBuffer, long nSize);
In article <EE**********************************@microsoft.co m>,
mv*@spam.guard.caspershouse.com says...
> What is the declaration of the function with the StringBuilder? It
shouldn't be "out", as the P/Invoke layer will know that the buffer has
to
be marshaled in and out when the StringBuilder is in place.



Why did you change the nSize into a long? it should be an int.
lpBuffer should be a "StringBuilder" not a "ref StringBuilder".
Willy.

Nov 4 '07 #4
PLS
In article <uU**************@TK2MSFTNGP03.phx.gbl>,
wi*************@telenet.be says...
"PLS" <no****@nowhere.comwrote in message
news:MP************************@msnews.microsoft.c om...
It was out, but changing it to ref did not change anything.

The definition is now
[DllImport("kernel32.dll", SetLastError=true,
CharSet=CharSet.Unicode,
CallingConvention = CallingConvention.StdCall,
EntryPoint = "GlobalGetAtomNameW")]
public static extern int GlobalGetAtomName(ushort nAtom,
ref StringBuilder lpBuffer, long nSize);
In article <EE**********************************@microsoft.co m>,
mv*@spam.guard.caspershouse.com says...
What is the declaration of the function with the StringBuilder? It
shouldn't be "out", as the P/Invoke layer will know that the buffer has
to
be marshaled in and out when the StringBuilder is in place.


Why did you change the nSize into a long? it should be an int.
lpBuffer should be a "StringBuilder" not a "ref StringBuilder".
Willy.

I was trying various things to get something to work. I ended up with
StringBuilder and int.

I've also corrected this function signature on pinvoke.net
Nov 6 '07 #5

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

Similar topics

4
by: Ted | last post by:
Is it possible to use mailslots in .NET using PInvoke? I have a VC++ 6.0 based app that creates and listens to a mailslot. I have a second VC++ 6.0 based app that opens the mailslot and writes...
5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
5
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD...
2
by: Craig | last post by:
I've seen many examples of how to call SHGetFileInfo in shell32.dll to get a files associated icon, but I can't find anywhere how to get the file information (size, last date modified, etc, etc)...
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
0
by: Benosham | last post by:
I have been playing around with trying to PInvoke GDI+ from C#, I made recently made the transition from C/C++ to C# and I really like the language, however being the old fashioned programmer I am I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...

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.