[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Cannot call simple WinApi function

Carl

8/15/2007 11:51:00 AM

Hi,

I've got stuck on calling the following WinApi function. I've tried lots of
different datatypes and read alot about which to use in the documentations,
but everything fails. Does anyone know how to do it?

The Call:

StringBuilder sb = new StringBuilder(255);
IntPtr handle = new IntPtr();
uint result = GetModuleFileName(handle, out sb, (uint)255);

I want to send "null" as the handle, to get the currently running process
path.

My import declaration:
[DllImport("Kernel32.dll")]
public static extern uint GetModuleFileName(IntPtr Handle, out StringBuilder
Filename, uint Size);

The MSDN documentation of the Api:

DWORD WINAPI GetModuleFileName(
HMODULE hModule,
LPTSTR lpFilename,
DWORD nSize
);

I hope someone can assist me with this!

regards

Carl


2 Answers

Carl

8/15/2007 12:15:00 PM

0

> I've got stuck on calling the following WinApi function. I've tried lots
> of different datatypes and read alot about which to use in the
> documentations, but everything fails. Does anyone know how to do it?
>
> The Call:
>
> StringBuilder sb = new StringBuilder(255);
> IntPtr handle = new IntPtr();
> uint result = GetModuleFileName(handle, out sb, (uint)255);
>
> I want to send "null" as the handle, to get the currently running process
> path.
>
> My import declaration:
> [DllImport("Kernel32.dll")]
> public static extern uint GetModuleFileName(IntPtr Handle, out
> StringBuilder Filename, uint Size);
>
> The MSDN documentation of the Api:
>
> DWORD WINAPI GetModuleFileName(
> HMODULE hModule,
> LPTSTR lpFilename,
> DWORD nSize
> );

Never mind, I got it to work by removing the "out" keyword!


wawang

8/16/2007 2:19:00 AM

0

Hi Carl,

FYI: you may find following document useful when using P/Invoke to call
win32 dlls:

#.NET: Calling Win32 DLLs in C# with P/Invoke -- MSDN Magazine, July 2003
http://msdn.microsoft.com/msdnmag/issues/...



Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.