473,326 Members | 2,655 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,326 software developers and data experts.

Win32-API function calls in VS2005 CLR applications

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
May 4 '07 #1
6 6179
>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
May 4 '07 #2
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:

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
May 4 '07 #3
R.Kaiser wrote:
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
May 4 '07 #4
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:
R.Kaiser wrote:
>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

May 4 '07 #5
R.Kaiser wrote:
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?
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.hand 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

May 4 '07 #6

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:e4****************@TK2MSFTNGP06.phx.gbl...
R.Kaiser wrote:
>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?

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.hand 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.
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

May 7 '07 #7

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

Similar topics

1
by: Brian Beck | last post by:
As far as I can tell, both maintainers of the Windows builds of Python-LDAP haven't updated in about a year. This doesn't exactly make the builds ancient or obsolete, but it would be nice to start...
4
by: Scaramouche | last post by:
is there an equivalent to unixs' ps command in perl? i was writing some win32 scripts and i needed a listing of specific processes. i could perhaps grep through a list of running processes but how...
1
by: RL | last post by:
Hi all, I am new and am lost with Win32::Process stuff. I want on perl script to start a process, then another perl script to terminate the same process. I can start a process using......
6
by: Pawel | last post by:
Ary you know tools to convert MSIL code (Assembly) to Win32 (not Just In Time)?
3
by: Gil | last post by:
I need to create a web page in which its content should be controlled by some win32 application. this application may add or remove some gif images from the page and it should be smooth as possible...
0
by: =?Utf-8?B?Q29saXZpZXI=?= | last post by:
If anyone can help me with this I would really appreciate it: I have an assembly into which I have linked a manifest file as a Win32 resource. This is necessary since I want to use a class in this...
8
by: rssd | last post by:
can somebody help me. I'm trying to read some excel files but i'm always getting this error No type library matching "Microsoft Excel" found at D:\Genes_datasets\exp.pl line 4 Win32::OLE(0.16):...
3
by: somuchh8 | last post by:
Hi, I'm having a lot of trouble with the Win32::Spawn module in perl. Here is my situation, I have a Win32::Spawn call which looks like this: my $success = undef; my $cmdline =...
1
by: user1357 | last post by:
Hi all, i am trying to run the following code use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel';
6
by: Perl Beginner | last post by:
Good morning all, I had to reinstall Perl (ActivePerl 5.10.0 Build 1004) on my computer (Windows XP), the install went fine. however, I'm having issues loading Win32-GuiTest using the repository in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.