|
That's an EXE, not a DLL, and can't be mapped into memory so you can't use it. Judging by the name, however, it looks like a self-extracting ZIP or installation program, so extract/install it and find the DLLs you need, which should be documented with the functions you need. Then see the DllImportAttribute documentation in the .NET Framework SDK for information about how to declare native functions using P/Invoke. You'll also find more information on MSDN by reading Consuming Unmanaged DLL Functions[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You have to import the functions using the DllImport attribute, like this:
[DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd);
But, of course, you have to know the function names ahead of time.
RageInTheMachine9532
|
|
|
|
|
Hello,
I am sure similar problem must have been here many times, but I am unable to find it, so I am writing about it again. I have a class SSocket, a child of System.Net.Sockets.Socket, the only difference is that it has two static methods, so in worst case it won't be problem to change the architecture a little.
When I retreive a client socket from socket.Accept() and try to cast it to my SSocket variable, exception is thrown, saying the cast is invalid. Can anyone say, what am I doing wrong?
This is how the code looks like:
class SSocket : Socket{
SSocket(..parameters..) : Socket (..same parameters as passed to child constructor..)
public static void GetCommand()
{...}
public static void SendCommand(...){...}
}
...
{
SSocket clientSocket = (SSocket)serverSocket.accept(); <- exception is always thrown
Thanks
John
|
|
|
|
|
The return, of course, is a Socket , not an SSocket , so you can't cast because it isn't that type. Casting to a parent always works, and in a polymorphic design if a method returns the parent class of a deriviative object but the object is a type of the derivative, you can then cast down, like so:
public class Parent
{
public abstract Parent ReturnMe();
static void Main()
{
Child child = (Child)new Parent();
Console.WriteLine(child.ReturnMe());
}
}
public class Child : Parent
{
public override Parent ReturnMe()
{
return this;
}
} That's not the case here.
I recommend encpasulating the Socket instead of deriving from it. This also gives you more control over what the caller can do so you only expose what you want to expose.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, I was already quite reconciled with encapsulating the Socket instead, but wanted to know the reason, why the downcasting wasn't possible.
So, to be sure I do understand it - the child class must not introduce any new members or methods in order to be able to accept it's parent?
John
|
|
|
|
|
It doesn't matter if all you do is extend a class and don't add anything more, it's still not the same Type (that's what it all comes down to). Take the following simplistic test:
using System;
public class Test
{
static void Main()
{
Test t = new Test();
Test2 t2 = (Test2)t;
}
}
public class Test2 : Test
{
} It compiles, but you get an InvalidCastException when run. The reason is that t is not an instance of Test2 , so you can't cast it. If I said Test t = new Test2() , then it'd work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a C(++) souce code line:
UserTri* pNewVariable;
...where UserTri is a class. All this should declare a new variable of UserTri type.
My question is: does this above equals to this below in C# ?
UserTri pNewVariable;
The "*" after the UserTri made me think that it might be a pointer, but then again in C# all classes are reference types so the "pointer operation" is actually just hidden here (in C#). True ?
|
|
|
|
|
|
If your C++ code is managed C++: yes, in fact it is. If your C++ code is unmanaged C++: no, it's not the same.
Pointers in managed C++ are managed by the garbage collector, as are the reference types in C#. Hence you don't have to (and you will not be able to)delete them manually. Pointers in unmanaged C++, however, are completely at your mercy, so you have to take care of them via the delete operator.
|
|
|
|
|
Yes (but maybe no, if UserTri is actually a value type (like struct) rather than 'true' class).
I'm an Assembler programmer at heart, and in cases like this, ask myself, "Is this storing an address (of, say, 4 bytes), or the value?" The C++ code is storing an address. The C# code stores address of classes & copy of variable for value types (simple data types & structs).
I LOVE pointers, and regard C#'s reference (and C++'s, for that matter) as 'pointers in disguise'.
Regards
Brewman
|
|
|
|
|
I am sending binary data between C# and C++ successfully. However, I would like to convert the C++ time_t, which is a 32 bit long in C++ to a C# DateTime. Does anyone have a solution off hand for converting time_t to a C# DateTime value and from a C# DateTime to a C++ time_t?
|
|
|
|
|
Since time_t is just an unsigned 32-bit integer, you can use uint (alias for UInt32 ), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use the MarshalAsAttribute with UnmanagedType.U4 . For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64 . The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this.
The biggest hurdle has to do with the epoch (start time) of each. time_t is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. A DateTime in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see.
One easy way to convert between the two is to create a DateTime that starts at the epoch of a time_t , and then add the number of seconds represented by time_t :
DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
dt.AddSeconds(time_t); If you do this a lot, you might consider storing the return from DateTime.Parse as a read-only field of your class (the readonly keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use a const for a non-constant value like this).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Two lines of code! Thank you much.
|
|
|
|
|
"For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64"
I think that you'll find that 0xffff is 65535 for Int32 as well as in UInt16, UInt32, Int64 & UInt64 but -1 in Int16.
0xffffffff is -1 in Int32 & 4,294,967,295 in UInt32, Int64 & UInt64.
Regards
Brewman
|
|
|
|
|
Oops, that last part of the quote was actually supposed to be UInt32 , but you're right - I obviously couldn't perform basic math that day.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi!
My code looks like this:
//----------------------------------------------------------------------------
input = new FileStream(tempDirectory + "\\" + tempArchiv.Dateiname, <br />
FileMode.Open);<br />
output = new FileStream(sicherungsOrt + "\\" + tempArchiv.Dateiname, <br />
FileMode.Create, <br />
FileAccess.Write);<br />
reader = new BinaryReader(input);<br />
writer = new BinaryWriter(output);
//here I read from the reader and write it to the file with the writer
//...
//then I flush and close the input, output, reader and writer
input.Close(); <br />
reader.Close();<br />
<br />
writer.Flush();<br />
output.Flush(); <br />
<br />
output.Close(); <br />
writer.Close();
//here I do other things, but I don't do anything with the files I wrote and read
//and then I try to delete the file I wrote, but it occurs an exception and I don't know why
System.IO.File.Delete(sicherungsOrt + "\\" + tempArchive.Dateiname)
//----------------------------------------------------------------------------
The Exception looks like this:
The process is not able to access the file, because an other process uses it.
What's wrong with my code?
thanks, Gerhard
|
|
|
|
|
The wrapper reader/writer classes will call the wrapped streams close/flush methods. I suggest u remove either and try again. Everything else looks fine to me.
top secret
|
|
|
|
|
Cool ... it works...
thank you!!!
|
|
|
|
|
One thing to add (since leppie already helped you solve the problem), use Path.Combine instead of concatenating your paths with separators yourself. This makes sure that the correct directory separate is used for the target platform without having to recompile. This makes your code more portable. Also, you can use the Path.DirectorySeparatorChar if you want to know which to use. Each platform's version of the CLR is responsible for returning the proper char . Path.Combine also takes into account whether the directory reference already ends in the appropriate directory separator. This usually isn't a problem, but you can never be too sure.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
Does anyone know of what tools/sdk are available which would allow me to write server software to stream mp3 audio over the internet?
Thanks very much~
Regards,
John
|
|
|
|
|
This is a message for people who have been developping missing collection in .Net (such as sets, deque, etc...) or are simply missing those for every day life.
The objective is collect in a single project a number of forgotten data structures for .Net.
If you are interrested to contribute, observe or test, you can register at http://ncollection.tigris.org[^]
Jonathan de Halleux - www.dotnetwiki.org -
MbUnit - QuickGraph
|
|
|
|
|
Jumping the gun a bit?
Messages
Your account does not have the "Project Page - View" permission needed for you to access the page you requested in the ncollection project (view your permissions). Either ask the project administrator for more permission, or log in using a different account.
You are currently logged in as leppie.
top secret
|
|
|
|
|
|
On a lighter side, once you have collected all these joints, who will smoke them?
top secret
|
|
|
|
|
I'm more a beer-drinker type but I'm sure I could find people interrested for that kind of collection traversal.
Jonathan de Halleux - www.dotnetwiki.org -
MbUnit - QuickGraph
|
|
|
|
|