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

replacement for #include?

I'd like have a file with all of my Win32API stuff in it:

namespace myStuff.InteropTypes
{
using HBITMAP = System.IntPtr;
using HWND = System.IntPtr;

public struct POINT
{
public int x;
public int y;
}
}

and then be able to do something like this in another file:

using myStuff.InteropTypes;
namespace myStuff.myProject
{
[DllImport( "user32.dll" )]
static extern bool SomeFunc( HWND hWnd, HBITMAP hBitmap, POINT point );
[...]
}

which doesn't work since the 'using' aliases from the first file aren't
visible outside that compilation unit. Is there another way to do this?

Michael Roper
Nov 15 '05 #1
6 1317
> namespace myStuff.myProject

Not that it matters for the question at hand, but that should instead be
"public class myClass" or some such.

Michael Roper

Nov 15 '05 #2
The only thing I can think of is rather ugly, but it should work, it uses an
implicit conversion, just directly using IntPtr would probably be far
better:
public struct HWND
{
HWND(IntPtr p)
{
ptr = p;
}
System.IntPtr ptr;
public static implicit operator HWND(IntPtr p)
{
return new HWND(p);
}
}

Maybe someone else has a better solution?
"Michael Roper" <mi******@encraft.com> wrote in message
news:bk************@ID-160215.news.uni-berlin.de...
I'd like have a file with all of my Win32API stuff in it:

namespace myStuff.InteropTypes
{
using HBITMAP = System.IntPtr;
using HWND = System.IntPtr;

public struct POINT
{
public int x;
public int y;
}
}

and then be able to do something like this in another file:

using myStuff.InteropTypes;
namespace myStuff.myProject
{
[DllImport( "user32.dll" )]
static extern bool SomeFunc( HWND hWnd, HBITMAP hBitmap, POINT point );
[...]
}

which doesn't work since the 'using' aliases from the first file aren't
visible outside that compilation unit. Is there another way to do this?

Michael Roper

Nov 15 '05 #3
Daniel O'Connell writes:
The only thing I can think of is rather ugly, but it
should work, it uses an implicit conversion, just
directly using IntPtr would probably be far better:


Thanks Daniel, I'll play with it and see how it looks in IL. You're right
though, it is ugly! Guess I was hoping for a "oh, here's how you do a text
include..."

I guess I don't understand why (since text includes are disallowed) the
"using <namespace>" and especially the "using =" statements can't actually
be compiled into a namespace (you know...so I could do what I want :).
Where's the harm?

Michael Roper
Nov 15 '05 #4

"Michael Roper" <mi******@encraft.com> wrote in message
news:bk************@ID-160215.news.uni-berlin.de...
Daniel O'Connell writes:
The only thing I can think of is rather ugly, but it
should work, it uses an implicit conversion, just
directly using IntPtr would probably be far better:
Thanks Daniel, I'll play with it and see how it looks in IL. You're right
though, it is ugly! Guess I was hoping for a "oh, here's how you do a

text include..."

I guess I don't understand why (since text includes are disallowed) the
"using <namespace>" and especially the "using =" statements can't actually
be compiled into a namespace (you know...so I could do what I want :).
Where's the harm?
Don't know if there is a specific harm, it just doesn't seem to be an
apparent usage. I don't know if the design team ever even thought that
someone may want to alias a type to another type within a specific
namespace(which, IMHO, is much better than a typedef or #define that applies
globally).
One thing I could see is its a bit confusing. I HATED C\C++ code where I'd
see declarations like this(hopefully correct, its been a while):

typedef DWORD unsigned int;
typedef UINT DWORD;
typedef MYHANDLE UINT;
typedef YOURHANDLE MYHANDLE;
//then to throw some really oddities in, what if they did this?
//is this even allowed...its been so long I can't remember..think it is.
#define YOURHANDLE unsigned char

I would then find a reference to YOURHANDLE somewhere, and not really have a
clue as to what it was. Having an exact type name can save alot of
frustration in the long run. Which is also a good part of why that code I
posted earlier is ugly as well, you still don't really know that HWND is
actually an IntPtr, or if looking at a method without intellisense or the
docs, that the method doesn't take a IntPtr anyway.

Michael Roper

Nov 15 '05 #5
Daniel O'Connell writes:
Which is also a good part of why that code I
posted earlier is ugly as well, you still don't
really know that HWND is actually an IntPtr,
or if looking at a method without intellisense
or the docs, that the method doesn't take a
IntPtr anyway.


Right, that's why I'd have a clear preference for the 'using =' construct if
it worked. It's really nice (especially as an old, old WinAPI guy) to be
able to see my PInvoke imports defined the way I've always used them, and
get the "truth" from Intellisense if I need it.

Michael Roper
Nov 15 '05 #6

"Michael Roper" <mi******@encraft.com> wrote in message
news:bk************@ID-160215.news.uni-berlin.de...
Daniel O'Connell writes:
Which is also a good part of why that code I
posted earlier is ugly as well, you still don't
really know that HWND is actually an IntPtr,
or if looking at a method without intellisense
or the docs, that the method doesn't take a
IntPtr anyway.
Right, that's why I'd have a clear preference for the 'using =' construct

if it worked. It's really nice (especially as an old, old WinAPI guy) to be
able to see my PInvoke imports defined the way I've always used them, and
get the "truth" from Intellisense if I need it.
It seems your options are pretty restricted, I can see that the type is of
use(and this is a situation where I would use hungarian notation, marking
variables with h or hwnd). However, such a feature would probably be abused
commonly for less useful reasons, similarly to how C libs often used extra
typedefs simply to change the name, it got annoying and started to really
get ridiculous Michael Roper

Nov 15 '05 #7

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

Similar topics

92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
53
by: Kerberos | last post by:
I followed Dan Cederholm's image replacement tutorial, to replace a header tag by a logo. The h1 is clickable if no CSS is applied but it I replace it by the logo, the area isn't clickable anymore...
3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
20
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read...
3
by: chris | last post by:
Hallo, I am in need of a replacement for the Microsoft Visual Studio .NET. The reason is quiet simple. I develop forms which are used on different microsoft windows platform, and one...
1
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .=...
10
by: woessner | last post by:
I'm using a C++ compiler on a DSP that doesn't provide the bitset class. Can anyone suggest a replacement for it? I've written a lot of code that uses the bitset class (before I knew about the......
15
by: Roman Mashak | last post by:
Hello, I'd like to make a simple replacement of 'pow()' function for the embedded platform I'm working on. What is the better way, probably bit shifting? Thanks. Best regards, Roman Mashak.
1
by: Francogrex | last post by:
Hi, I'm trying to sample without replacement some numbers (xons:70 values out of 1 to 200 and xEPS1cover:150 values out of 1 to 200). Then I'm trying to intersect both samples to see how many are...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.