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 / Languages / Managed C++ / January 2008

Tip: Looking for answers? Try searching our database.

How do I call Win32 API functions SetClipBoardViewer, ChangeClipboardChain, etc, from C++ .NET app ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas Dickens - 25 Jan 2008 02:37 GMT
   
Hi,

I am a "hobby" programmer with a lot of familiarity with pre-.NET C++
programming in Windows, but not too much with .NET. I've written a
Form based Windows application and want to set it up so that the app
monitors changes to clipboard data. Therefore, I need to call the
Win32 functions SetClipBoardViewer, etc. (Or is there another way to
do this in .NET that I haven't found)?

As far as I can tell, I need to use the P/Invoke mechanism with
declarations something like:

[DllImport("User32")]
static int SetClipboardViewer(int hWndNewViewer);

(I would think these should be HWND's, but the above is what I found.)

My problem is that I don't know the correct declarations for these
functions, and I don't know how to set up the calls to them. I found
an example for C# that shows how to do the whole process of setting up
a clipboard monitoring application, but I really want to learn how to
do this in C++.

Any help would be appreciated!
Thanks,
Tom
David Lowndes - 25 Jan 2008 09:31 GMT
>I am a "hobby" programmer with a lot of familiarity with pre-.NET C++
>programming in Windows, but not too much with .NET. I've written a
[quoted text clipped - 5 lines]
>As far as I can tell, I need to use the P/Invoke mechanism with
>declarations something like:

Tom,

The beauty of using C++/CLI as your .Net language is that you often
don't need to use visible kludges like P/Invoke to call native code -
just #include the appropriate header (windows.h) as you've always
done, and write the code. Have a look at "Using C++ Interop (Implicit
PInvoke)" in MSDN.

Dave
Tamas Demjen - 25 Jan 2008 22:07 GMT
> [DllImport("User32")]
> static int SetClipboardViewer(int hWndNewViewer);
>
> (I would think these should be HWND's, but the above is what I found.)

How so? It is an HWND in the Win32 API:
http://msdn2.microsoft.com/en-us/library/ms649052.aspx

The problem with using int is that it is always a 32-bit value. HWND is
32-bit on 32-bit systems, but 64-bit on 64-bit systems. So your code is
not portable. It is a very bad idea to marshal pointers as integers,
because when the time comes to port to x64, it will crash.

I agree with David's answer, but if you must use P/Invoke, declare it
IntPtr instead of int:

[DllImport("user32.dll")]
static IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

Tom

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.