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++ / May 2007

Tip: Looking for answers? Try searching our database.

Win32-API function calls in VS2005 CLR applications

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
R.Kaiser - 04 May 2007 14:13 GMT
I know that I can call Win32 API functions in a Windows forms
application by specifying each function header individually like in

    using namespace System;
    using namespace System::Runtime::InteropServices;

    typedef void* HWND;
    [DllImport("user32.dll", CharSet = CharSet::Ansi)]
    extern "C" int* MessageBox(HWND hWnd, char* pText,
            char* pCaption,unsigned int uType);
    void test_Win32Messagebox_1(void)
    {
    char* pText = "Hello World!";
    char* pCaption = "Platform Invoke Sample";
    MessageBox(0, pText, pCaption, 0);
    }

What I want to know: Is there an easy way to include all headers by
using windows.h or a similar header file:

    extern "C" {
     #include <windows.h>
      }

I tried to combine this #include statement with a DLLImport attribute
like from above, but I did not succeed.

Thanks
Richard
David Lowndes - 04 May 2007 17:02 GMT
>What I want to know: Is there an easy way to include all headers by
>using windows.h or a similar header file:

Richard,

A C++/CLI program can mix managed/unmanaged calls seamlessly - no need
for anything nasty - so if you need to interface with lots of native
code, C++/CLI is a much cleaner solution.

Dave
R.Kaiser - 04 May 2007 17:31 GMT
Hi David,

I was talking of C++/CLI applications. Can you give me an example how to
do it?

Thanks
Richard

David Lowndes schrieb:
>> What I want to know: Is there an easy way to include all headers by
>> using windows.h or a similar header file:
[quoted text clipped - 6 lines]
>
> Dave
Carl Daniel [VC++ MVP] - 04 May 2007 18:27 GMT
> Hi David,
>
> I was talking of C++/CLI applications. Can you give me an example how
> to do it?

#pragma managed(push, off)
#include <windows.h>
#pragma managed(pop)

No DllImport attributes are needed.  Make sure you're linking with the
appropriate import library(ies) (e.g. kernel32.lib, user32.lib, etc).

-cd
R.Kaiser - 04 May 2007 19:06 GMT
Thanks Daniel,

perhaps I confused C++/CLI and CLR applications. I need the Win32-API
functions in a CLR Windows forms application. When I proceed as you
suggested, I get numerous errors like

    Carl DanieC:\Program Files\Microsoft Visual Studio        
    8\VC\include\vadefs.h(89) : error C3862: '__va_start':
    cannot compile an unmanaged function with /clr:pure or /clr:safe
        #pragma unmanaged is in effect

Or did I confuse something else?

Richard

l [VC++ MVP] schrieb:
>> Hi David,
>>
[quoted text clipped - 9 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 04 May 2007 20:29 GMT
> Thanks Daniel,
>
[quoted text clipped - 8 lines]
>
> Or did I confuse something else?

You need to understand what the error message is telling you:  You can't
call unmanaged functions, except via P/Invoke, in a "pure" or "safe" CLR
project.  You have two choice:

1. Don't use /clr:pure or /clr:safe, just use /clr
2. Don't #include <windows.h> and go back to using DllImportAttribute one
import at a time.

If you take option 1, the resulting application will contain a mixture of
managed and unmanaged code and may not run in certain contexts (e.g. partial
trust environment). Winforms will work fine in a mixed-mode application -
it's entirely a function of the environment in which the application is
deployed whether this option will work for you.

If you take option 2, you're stuck with declaring the functions you need one
by one.

-cd
Ben Voigt - 07 May 2007 15:07 GMT
>> Thanks Daniel,
>>
[quoted text clipped - 25 lines]
> If you take option 2, you're stuck with declaring the functions you need
> one by one.

And still won't work in partial trust... because you need native code
permission to use P/Invoke.  However you'll get a runtime exception instead
of an outright failure to load.

Best of both worlds... put such calls in a separate unsafe assembly, then
you can still fail gracefully if you don't have native code permission.

> -cd

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.