|
If I want my AP be only one ? How can I do this in C#?
There is an example in VC http://www.codeproject.com/threads/singleinstance.asp
|
|
|
|
|
VC apps usually take advantage of mutexes. Go in the .Net framework MSDN doc, search "Mutex". Finish the implementation.
And I swallow a small raisin.
|
|
|
|
|
When I saw this, I immediately thought of 'Highlander'.....
Christian
We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum )
Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
|
|
|
|
|
|
|
What do you mean by "doesn't like"?
Paul
|
|
|
|
|
it does not compile, as the function definition is as follows.
ChangeDisplaySettings(ref DEVMODE d, int flags);
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
DEVMODE is a struct.
1. just make another definition
ChangeDisplaySettings(object dummy, int flags);
or
2. change DEVMODE to a class with a sequential layout. Then you can pass null.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
|
I sent you VS solution, that happened to me too, seemed to be due not changing the enum declaration from ref DEVMODE to just DEVMODE ,its a class now so we dont need to. I however had to add the OutAttribute the the declaration.
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
How about making the function unsafe ?
Then use a pointer as parameter.
But that's maybe not possible when it's an imported function!?
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
have geen trying to get he current displaymode but have been unable, my latest attmpt was to try[^] and convert this articles code, but i had a little difficulty. I found the content of the DEVMODE struct on MSDN and put it into my code. I declare the EnumDisplaySettings like this
[DllImport("User32.dll")]<br />
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, DEVMODE lpDevMode);
then use it in code like this:
DEVMODE dm = new DEVMODE();
EnumDisplaySettings(null, -1, dm); but i get the exception 'object reference not set to an instance of an object' yet clearly, a DEVMODE object has been initiated.
any help would be appreciated.
*sudden thought* in the decleration of the DEVMODE struct, on MSDN, a couple of the members where of tpye TCHAR , declared like this.
TCHAR dmDeviceName[CCHDEVICENAME];<br />
TCHAR dmFormName[CCHFORMNAME];
i couldnt find what CCHFORMNAME is so i just used the MarshalAs Attribute like this
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string dmDeviceName; for both, could that be the problem?
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
I think the exception lies with string lpszDeviceName being null.
You mite want to redefine:
[DllImport("User32.dll")]
public static extern bool EnumDisplaySettings(StringBuidler lpszDeviceName, int iModeNum, DEVMODE lpDevMode);
Then when calling do StringBuilder devname = new StringBuilder( insert const size here).
Also , many times you need to set the size of a struct. In this case, according to MSDN, you will need to.
Nnamdi Onyeyiri wrote:
i couldnt find what CCHFORMNAME is so i just used the MarshalAs Attribute like this
This you will find the value in the header files, so download the Platform SDK if you dont have it already.
Hope this helps
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
leppie wrote:
I think the exception lies with string lpszDeviceName being null.
i dont think so, i tried entering a value for that, and it did not work, it gavethe same exception. i used null, because that is what was used in the article i gave the link for.
leppie wrote:
This you will find the value in the header files, so download the Platform SDK if you dont have it already.
is that installed with the default settings for vs.net install?
leppie wrote:
Also , many times you need to set the size of a struct. In this case, according to MSDN, you will need to.
i didnt know about this, ill have a look now.
Email: theeclypse@hotmail.com URL: http://www.onyeyiri.co.uk "All programmers are playwrights and all computers are lousy actors."
|
|
|
|
|
Nnamdi Onyeyiri wrote:
dont think so, i tried entering a value for that, and it did not work, it gavethe same exception. i used null, because that is what was used in the article i gave the link for.
Still try using StringBuilder as u CAN pass null as a value.
Nnamdi Onyeyiri wrote:
is that installed with the default settings for vs.net install?
If u install VC++ , else just look for a dir in program files with many *.h files
I had a look and that is a pretty viscious struct . Im not sure what a BCHAR is though. Im gonna try do something in C# and see what i come up with. Can you perhaps mail me what u have done so far?
Cheers
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
|
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
int mode = 0;
while (EnumDisplaySettings(null, mode++, ref dm) != 0)
{
Console.WriteLine("{0}x{1}, {2}bits {3}hz",
dm.dmPelsWidth, dm.dmPelsHeight, dm.dmBitsPerPel, dm.dmDisplayFrequency);
}
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
static extern int EnumDisplaySettings(StringBuilder devname, int mode, ref DEVMODE devmode);
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
This works Note the union ???? looks like it combines 2 int 's ???
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
|
Im not really sure what a union in C++ does, it appears to join 2 variables into one, hence the commented dmNup. Can anyone C++ person please explain this in C# terms?
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
A union uses the same piece of memory for two different variables.
For example
struct INTCHARS
{
char hi;
char midhi;
char midlo;
char lo;
};
union FOURBYTES
{
INTCHARS ch;
int i;
};
FOURBYTES four;
four.ch.hi = 1;
four.ch.midhi = 1;
four.ch.midlo = 1;
four.ch.lo = 1;
four.i = 0;
Now each part of four.ch is 0.
Going back a while since I used one of these.
Paul
|
|
|
|
|
Thx,
From what i can see is that unions are class like structures in a struct or class allowing easy retriaval/setting of inner variables. I have been using SequintialLayout classes todo this. Im not sure if we are talking the same thing, but I guess thats why the are not included in C#.
Cheers
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
The big difference between a union and a struct is that changing one inner variable in a union will change all of them because they share the same space in memory.
As C++ came into play, unions became less useful, classes and pointers could be used to the same effect.
When PCs with more than 50 bytes of spare memory came out, unions again lost some usefulness.
Likewise properties haven't done unions any favours, you can now reference the same variable in different ways through get/set properties.
So I wouldn't be surprised if C# didn't have an equivalent, though there are times when it wouldn't go amiss.
Paul
|
|
|
|
|
From what I understand (I also was looking into unions recently -- trying to DllImport DnsQuery_A) a union is a UDT which allows for one datatype or another, so if you specify it with a String or Int32 it would let the value be one of the two. At least that was what I took it to mean
I'm not aware of a native C# equivalent but would be glad to know of one!
--
Paul
"I need the secure packaging of Jockeys. My boys need a house!"
- Kramer, in "The Chinese Woman" episode of Seinfeld
MS Messenger: paul@oobaloo.co.uk
Sonork: 100.22446
|
|
|
|
|
As others have said a union just uses the same memory space to hold each of the types (thus only allowing one of them to be used at a time).
Typically when you are dealing with a union you have to break it out into its own datatype so that you can properly use it in code. Otherwise you could be having to use the same variable for two vastly different things such as a pointer to a string and a byte which will be hard to do when dealing with a single member.
To create a union in C#:
[StructLayout(LayoutKind.Explicit)]
public struct myUnion
{
[FieldOffset(0)] string FieldA;
[FieldOffset(0)] bool FieldB;
[FieldOffset(4)] int FieldC;
[FieldOffset(4)] long FieldD;
[FieldOffset(12)] int Foo;
[FieldOffset(12)] char Bar;
[FieldOffset(12)] float Baz;
} The numbers used will position where in memory each field will be placed, using the technique you can also create multiple unions as I did above (FieldA and FieldB; FieldC and FieldD; and Foo, Bar, and Baz; are all unions, where only one of the members can be used).
Hope that makes sense.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
That is very cool, James, nice one!
Paul
|
|
|
|