473,799 Members | 2,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ::InteropServic es;
typedef void* HWND;
[DllImport("user 32.dll", CharSet = CharSet::Ansi)]
extern "C" int* MessageBox(HWND hWnd, char* pText,
char* pCaption,unsign ed int uType);
void test_Win32Messa gebox_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 6201
>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\va defs.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\va defs.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 DllImportAttrib ute 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.nos pam>
wrote in message news:e4******** ********@TK2MSF TNGP06.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\v adefs.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 DllImportAttrib ute 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
2198
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 fresh with the latest version. I've tried without success in building it myself. The hardest part is simply that Python-LDAP is dependent on at LEAST two other projects -- OpenLDAP and Cyrus SASL. This increases the difficulty three-fold. ...
4
8467
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 can i attain that through perl? thanks
1
4976
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... ------------------------------------------------------- use Win32::Process;
6
2007
by: Pawel | last post by:
Ary you know tools to convert MSIL code (Assembly) to Win32 (not Just In Time)?
3
1517
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 as can. Can someone here direct me how to start that. Thanks in advance Gil
0
1931
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 assembly with registration-free COM interop from a Win32 application. I also want to use this assembly with a .Net application that I want to deploy using ClickOnce. However when I try to install the application from the deployment location I get...
8
36597
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): GetOleTypeLibObject() Not a Win32::OLE::TypeLib object at C:/Perl/site/lib/Win32/OLE/Const.pm line 45. Died at D:\Genes_datasets\exp.pl line 6. i don't ve microsoft excel but open office installed, i think that could be a problem. But these...
3
4105
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 = EBDTools::os_path("${nh_home}/web/aview/modules/svcrsp-ng/saSync.pl"); $success = Win32::Spawn($^X, "${^X} $cmdline ${optfile}", $pid); if (! $success) { my $lasterr = Win32::GetLastError(); return_configerror("Failed to create commit / sync process...
1
4521
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
10055
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 PPM. the steps i took are: 1. saved Win32-GuiTest-1.50.5.zip from the search.cpan.org website 2. unzipped in the location c:/Perl/Win32-GuiTest 3. opened the PPM window and went to Edit-Preferences and clicked the repositories tab 4. i put...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.