473,385 Members | 1,341 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,385 software developers and data experts.

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("kernel32.dll", SetLastError = true)]
static extern unsafe System.IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

Jan 21 '07 #1
1 6094
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
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...
3
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...
21
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
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
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...
3
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...
16
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
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...
1
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.