473,803 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing enum to pInvoke api call

when I call a win32 api like CreateFile, I would like to be able to
pass an enumerated value as the parameter value:
hFile = CreateFile(
InPath, GENERIC_READ,
ShareMode.Read, // this is an enum
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

I know I can cast the enum:
(uint) ShareMode.Read

but I consider that redundant ( and casting makes me nervous! ).

In C#, can I specify that the enum can always be converted to a (uint),
allowing the compiler to implicitly case the enum to a uint?

thanks,

-Steve

public enum ShareMode
{
NotShared = 0,
Read = 0x00000001,
Write = 0x00000002,
Delete = 0x00000004
}

[DllImport("kern el32.dll", SetLastError = true)]
static extern unsafe System.IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess ,
uint dwShareMode,
IntPtr lpSecurityAttri butes,
uint dwCreationDispo sition,
uint dwFlagsAndAttri butes,
IntPtr hTemplateFile);

Jan 21 '07 #1
1 6127
Well, I can't remember off-hand, but have you tried declaring the enum
as " : uint" and declaring the dll call with "ShareMode shareMode"
instead of "uint shareMode"? If this doesn't work, another option is to
at least hide it from the outside world by having the PInvoke call
private, and having all public versions accept the enum, which you then
cast when calling the dll. However, there is no reason for this cast to
make you nervous - especially when dealing with PInvoke this is fairly
common.

Specific to this problem, have you checked you can't do what you want
with File.Open / File.Create and the associated overloads (looking
especially at the FileMode and FileShare options)?

Marc

Jan 22 '07 #2

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

Similar topics

20
4852
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
3
4147
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API calls are needed! // using System; using System.Runtime.InteropServices;
21
4609
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
7
1600
by: tony collier | last post by:
if i have a string eg string day="monday"; and an enumerator enum days {monday=0,tuesday, wednesday}
3
1288
by: equip200 | last post by:
Hello All, I would like some assistance with an issue I have. I will do my best to explain it. I have 2 programs , one is called "AMI" and it could use VBscript to execute procedures . the other is "SAG" which I built in C#. can I create scripts to pass values to SAG from AMI? or do I need to create a plugin in SAG and have AMI call those procedures in the DLL? If plugin is the best option how can approach it? Your help would be...
3
3470
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is that my form appears to be blocked while the background thread is running. I am very familir with threads in unmanaged code but am just getting into them in C#. Are there issues I need to be aware of with regards to threading a simple pinvoke...
16
82856
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
6
4622
by: mRbEn | last post by:
Hi: I'm trying to write a web page by using c# under dotnet framwork. My target is lets the user to choose a csp to get a certificate from CA. But, I only know how to enum providers by cryptoapi like: CryptEnumProviders(...). Who can tell me how to enum csps by using C#? thanks
1
5248
by: Powers | last post by:
I currently have a large number of enums implemented into my Web Service. I am reworking the XML serialization of these enums. At present, each enum has hardcoded values, for example: xAttrs = New XmlAttributes xEnum = New XmlEnumAttribute xEnum.Name = "0" xAttrs.XmlEnum = xEnum
0
9703
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
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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
10295
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,...
1
7604
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
5500
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...
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.