Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / May 2006

Tip: Looking for answers? Try searching our database.

Tool Help API and interop (C#)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ziphnor@gmail.com - 26 May 2006 10:06 GMT
Hi,

I am trying to call the Tool Help API (
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/to
ol_help_functions.asp

) from C# using interop.

I have declared:

       [DllImport("Kernel32.dll")]
       private static extern IntPtr CreateToolhelp32Snapshot(UInt32
dwFlags, UInt32 th32ProcessID);

       [DllImport("Kernel32.dll")]
       private static extern bool Thread32First(IntPtr hSnapshot, ref
THREADENTRY32 lpte);

The THREADENTRY32 struct is declared as:

       [StructLayout(LayoutKind.Sequential)]
       public class THREADENTRY32
       {
           public UInt32 dwSize;
           public UInt32 cntUsage;
           public UInt32 th32ThreadID;
           public UInt32 th32OwnerProcessID;
           public Int32 tpBasePri;
           public Int32 tpDeltaPri;
           public UInt32 dwFlags;
       }

On the C side its:
typedef struct tagTHREADENTRY32 {
 DWORD dwSize;
 DWORD cntUsage;
 DWORD th32ThreadID;
 DWORD th32OwnerProcessID;
 LONG tpBasePri;
 LONG tpDeltaPri;
 DWORD dwFlags;
} THREADENTRY32,
*PTHREADENTRY32;

I call CreateToolhelp32Snapshot with the current process ID and
TH32CS_SNAPTHREAD (=0x4) and get something which appears correct(ie the
handle returned is not equal to INVALID_HANDLE_VALUE(=-1) and
GetLastError returns 0).

However when i then call:
           THREADENTRY32 te = new THREADENTRY32();
           Int32 size =
System.Runtime.InteropServices.Marshal.SizeOf(te);
           te.dwSize = (uint)size;
           bool ok = Thread32First(h, ref te); // h is return value of
CreateToolhelp32Snapshot

ok is false and GetLastError is 24 which translates to:
ERROR_BAD_LENGTH ("The program issued a command but the command length
is incorrect.").

Im guessing something is wrong with the struct i am passing in, but
what? I have checked that dwSize is set to 28 which is correct as far
as i can tell. Any help is appreciated.
Mattias Sjögren - 26 May 2006 19:09 GMT
>and GetLastError returns 0).

Don't call GetLastError from managed code. Add SetLastError=true to
the dllImport attribute and then call Marshal.GetLastWin32Error
instead.

>Im guessing something is wrong with the struct i am passing in, but
>what?

It's a class, not a struct. That's the problem. Change it to a struct,
or remove the ref modifier on the parameter.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

ziphnor@gmail.com - 27 May 2006 22:04 GMT
> >Im guessing something is wrong with the struct i am passing in, but
> >what?
>
> It's a class, not a struct. That's the problem. Change it to a struct,
> or remove the ref modifier on the parameter.

I feel like an idiot :) This solved the problem, thanks.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.