|
|
|
i know this doesn't apply if some values of your array were already changed, but the runtime asures you, that after constructing everything is set to zero.
thus
byte [] b = new byte[20];
for (int i=0; i<20; ++i)
if (b[i]!=0) MessageBox.Show("you will not see me");
runs without popping up that window.
:wq
|
|
|
|
|
And on top of all the other responses, it should be pointed out that you can always reinitialise any array with the Initialize method.
For example:
byte[] array = new byte[20];
for (int i=0;i<20;i++)
array[i] = i;
array.Initialize();
OR
string[] array = new string[20];
for (int i=0;i<20;i++)
array[i] = i.ToString();
array.Initialize();
One of these answers should help now
Paul
|
|
|
|
|
You should be able to make an "unsafe" call once you import it, something like this:
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
unsafe public static extern void ZeroMemory(System.IntPtr ptr ,uint size);
HTH
Nick Parker
|
|
|
|
|
hi, i'm stuck.
i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like
int foo(void *ppMessage);
the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two ps in the name)
in c++ it would be something like
byte *result;
foo(&result);
(*result)[0]
i have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged
thx in advance
:wq
|
|
|
|
|
ah. just found the Marshal-class - maybe this helps me...
:wq
|
|
|
|
|
First of all, are you sure about the signature of your C method ?
Isn't it more like this :
int foo(void **ppMessage);
To expose this C function to managed code, you need to declare it as :
[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
public static extern int foo(out IntPtr ppMessage);
where user32.dll must be replaced with the actual DLL name.
And I swallow a small raisin.
|
|
|
|
|
erm no, i looked it up in the header-file and there's only one star. i'm a bit confused too, but it seems you have to know, that you get a pointer in the specified location.
actually i have solved the thing like this:
[DllImport("theold.dll")]
public static extern Int32 foo(IntPtr [] messagePtr);
...
IntPtr [] messagePtr = new IntPtr[1];
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr[0]);
byte [] message = new byte[size];
Marshal.Copy(messagePtr[0],message,0,size);
i know (from the specification) that the first 2 bytes contain the size of the result in bytes.
:wq
|
|
|
|
|
Hi, it seems you should be able to remove the array like:
[DllImport("theold.dll")]public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,2,size);
I'm interested, does this work too?
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
1. yep. out works indeed. good idea. but in the foo-call is to be inserted uswell.
2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes)
finally:
[DllImport("theold.dll")]
public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(out messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,0,size);
thx again for your help an ideas
:wq
|
|
|
|
|
Rüpel wrote:
but in the foo-call is to be inserted uswell.
oops , happens when u write the code out of your head straight into the textbox
Rüpel wrote:
2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes)
But wont the fist 2 byte values (being a size value) affect the byte[]?
(oops another typo , meant to do this)
byte [] message = new byte[size - 2];
Marshal.Copy(messagePtr,message,2,size);
or
byte [] message = new byte[size - 2];
Marshal.Copy(new IntPtr(messagePtr.ToInt32() + Marshal.SizeOf(typeof(byte))*2),message,0,size - 2);
I'm just interested
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
I want to know wheather we can write components using c# in place of ATL COM.i.e..is it possible to write everything whatever we do in ATLCom.
|
|
|
|
|
It depends if you intend to consume these components from unmanaged or managed code.
If that's managed code, then you are talking about the whole .NEt platform. A C# user control exposes interfaces that can be used in a VB.Net class.
If that's unmanaged code :
You'd better have good reasons to do so. ATL/COM components do not require the .Net framework to work.
And yes, there are tutorials about this in MSDN. You usually end up by doing a tlbexp.exe or regasm.exe (which is the opposite of importing a COM type-library).
And I swallow a small raisin.
|
|
|
|
|
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++!
|
|
|
|