|
Well, i was thinking, if i allocated 2 bytes to global memory, get the intptr to them, forward them to the char array, and read it out as a char (not sure how to cast that part). I could see if it works with one char.
Following that, if i create a block of memory say, 100 bytes, i could then read each of the byte pairs out, to get a total of 50 chars?
Again, i'm not sure how to cast the two bytes, but i'll have a tinker.
As for what i'm doing. I was looking at how easy it would be to implement managed to unmanaged drag and drop. Mainly handling a stream of bytes.
Turns out, not so easy, but really interesting anyhow, and a valuable learning experience.
Cata
|
|
|
|
|
Putting data in a SAFEARRAY isn't always an option - you can't be sure what's in an IStream unless you're explicitly putting it there.
Using the correctly marshalling attributes is the first step. Again, keep in mind that a pointer to an array is just the address of the first element. If you look at ALL the native APIs that use arrays (even those that use LPTSTR , which is nothing more than array of TCHAR s), they always require an [in] or [in/out] specifying how many elements to expect. This is because you get the address of the first element and you keep offsetting the address by the bit width of the processor (typically) X number of times where X is the number of elements.
Also keep in mind that an array is already a reference type (even if it is an array of value types), so including the out or ref keywords is often unnecessary. Pass an instance of the array. Take, for instance, the GetComputerName API (off the top of my head):
BOOL GetComputerName( LPTSTR lpBuffer, LPDWORD lpnSize); In .NET, you'd simply declare and use it like:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern bool GetComputerName(string buffer,
[MarshalAs(UnmanagedType.U4)] ref int size);
int size = 16;
string buffer = new string(size, '\0');
GetComputerName(buffer, ref size); The same is true for arrays.
The problem is once you get your array of bytes (don't ever assume their characters), now you must get it into a managed object or read/write it as is.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I think i've been doing something horendously retarded. I've been trying to marshal my arrays using ref and out. It didn't work. That's been a major problem with my way of thinking.
I just ditched the ref, and it's working... kind of.
I can see the problem with my char array. I've declared an array of char (that's all i'm worried about at the moment, byte buffers can wait until i get to that point, for my test porgram i know it's going to be chars.) Now, i send this array of 10 char elements in, and marshal it as an LPArray of size 10. However, i'm getting 5 elements in the array of 4 bytes in size, (They are all over 127 value). The rest is blank.
How do i get it to use Uni instead of Ansi charachters?
I tried your idea with strings, but all i get back in the string, regardless of the number of elements i ask for (and duly get), is 4 blank charachters, this is the same for LPStr and BStr marshaling. Not entirely sure why.
Cata
|
|
|
|
|
First, don't marshal a char array as an array - marshal it as the appropriate string (since a string is a char array anyway). When you get a byte buffer back, you can use the appropriate Encoding class to get the string from the bytes.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath is right, of course (damn you Heath! )
When you pass a reference type from unmanaged space through the interop layer, it automagically pins or copies the referenced value types so you can work with them in the managed space.
Having a second to peek through MSDN, however, turned this up:
UCOMIStream
Which may, in fact, be exactly what you're looking for...
Jeremy Kimball
|
|
|
|
|
lol!
bugger - all that work
oh well, at least i know I can marshal IStream....
Cheers mate
Cata
|
|
|
|
|
*sticks tongue out at Heath* There! *I* answered one for once!
[EDIT:] BTW Heath, wasn't around to congratulate you on your "MVP-ness"[/EDIT]
|
|
|
|
|
Oh yeah...well...well...I have over 3,400 posts! Hah!
Thanks, BTW!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
This really sucks.
It doesn't give you a valid result for how many elements were retrieved, always a zero IntPtr, so you don't know when you reach the end of the stream, and you can't collect the error value because it returns void.
wtf are Microsoft playing at?
I have to copy the buffer on each iteration, and compare the array of bytes now to see if they are identical. THEN i have to figure out where the stream ends in the frickin buffer. That's LAME man. I did a better job than that. *GRRRR*
Cata
|
|
|
|
|
|
I've decided i'm going to stick with my own implementation, as having no value is of little use. The resultant intptr from read is always zero (null). I spent a while with it, but MS descided that no return value was nescessary... lovely.
I've got it so that it reads the byte array, and converts it into a string. Nifty. I am a very happy little computer geek
Here is what I have:
pIStream.Seek(pInZero,STREAM_SEEK_Flags.STREAM_SEEK_SET, out pSeekResult);<br />
<br />
sbyte[] buffer = new sbyte[bufferSize];<br />
<br />
string charReader = "";
int read;
<br />
do<br />
{<br />
pIStream.Read(buffer, bufferSize, out read);<br />
<br />
if (read != 0)<br />
{<br />
unsafe<br />
{<br />
fixed(sbyte* pChars = buffer)<br />
{<br />
charReader += new string(pChars,0,read,System.Text.Encoding.ASCII);<br />
}<br />
}<br />
}<br />
}while (read != 0);
it works fine, reading huge text documents.
I'm interested as to what else I can do with it.
I think i need to start building a decent layout, my test program has got pretty big, and ugly as sin. That said... IT WORKS! ^^
Cata
|
|
|
|
|
If you don't want to convert the VB.NET code yerself, here's a quick try at it:
UCOMIStream stream = ...;
int offset = ....;
int count = ....;
byte[] resultBuffer;
if( stream == null ) throw new ObjectDisposedException("stream");
int bytesRead = 0;
object boxBytesRead = bytesRead;
GCHandle hObject;
try {
hObject = GCHandle.Alloc(boxBytesRead, GCHandleType.Pinned);
IntPtr pBytesRead = hObject.AddressOfPinnedObject();
if( offset != 0 ) {
byte[] tmpBuffer = new byte[count - 1];
stream.Read( tmpBuffer, count, pBytesRead );
bytesRead = (int)boxBytesRead;
System.Array.Copy( tmpBuffer, 0, resultBuffer, offset, bytesRead);
} else {
stream.Read( resultBuffer, count, pBytesRead );
bytesRead = (int)boxBytesRead;
}
} finally {
if( hObject.IsAllocated() ) {
hObject.Free();
}
}
}
Not tested, of course....
Jeremy Kimball
|
|
|
|
|
I take it by doing it the way i posted above, i'm getting an int from a pointer, and not handling it properly.
That will lead to memory leaks? Unless i unallocate it?
Cata
|
|
|
|
|
I hope I can explain this well enough to make sense, I am writing a C#/ASP app where I have a grid (4x3) of buttons on a page. Each one is named btnMonth1, btnMonth2 - btnMonth12 I am working with some database stuff where they may only be working on certain months at a time so depending on what number is in the data I would like to make it visible... so if I had a variable mth I would want to somehow say btnMonth(mth).visible = true; AFAIK in C++ you can do this by incrementing it but I don't think forms use resource ID's so I am not sure this would work.
I'm trying to learn something at the same time while trying to cut a big switch statement out of the mix that seems unnecessary. Maybe that's how it should be done though?
Any help is appreciated
Thanks!
|
|
|
|
|
Is this on the web page? If so, you can use:
System.Web.UI.Control.FindControl(string id)
If it's on a windows form, you'd probably have to do some funky iteration using GetNextControl or something...
Jeremy Kimball
|
|
|
|
|
Yes it's on a webpage. Thanks! I'll give this a try!
|
|
|
|
|
I am working with very limited screen space and I would like to add a toolbar that just has text but no images. If I leave the image list null, space is still set aside for the images.
Can I use the Toolbar control without images and without the control defaulting space for the image?
thanks
|
|
|
|
|
can't you just use a mainMenu bar? otherwise try altering the default width setting of the buttons - ButtonSize. hwo about turning off the AutoSize control? don't know if this is any help...
looking for hosting? ithium is good.
|
|
|
|
|
Hi every body!
please tell me, how can i get addresses from outlool express?
Thanks!
|
|
|
|
|
not sure but the address book file seems to be:
C:\Documents and Settings\[user]\Application Data\Microsoft\Address Book\[user].wab
don't know if this will help!
looking for hosting? ithium is good.
|
|
|
|
|
You can add a COM reference to the Outlook type library and interop it; not sure what's in there as far as an API goes. I haven't played with it for some time.
Jeremy Kimball
|
|
|
|
|
Hi, everyone
I have a dll file developed by someone else in VC6. Now I tried to use it in VS.Net, but how can I do that?
Thanks in advance.
|
|
|
|
|
If it is a COM dll, you can just use Add Reference meny by right click on your references in your project solution and select its dll in COM tab and simply use its namespace. If its not COM and its just like win32 dlls you can use DllImport to use its functions. See MSDN and this site for samples.
Mazy
"Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland
|
|
|
|
|
It's not a com dll. Because when I am using "add reference", it doesn't work. Then I tried to use DllImport like this:
[System.Runtime.InteropServices.DllImport(@"C:\Visual Studio Projects\test\bin\Debug\dcli.dll", EntryPoint="AdminLogin")] <br />
public static extern bool AdminLogin(string lpctstrUser, string lpctstrPassWord);
Then I got a "DllNotFound" exception. But I already put the dll file in "C:\Visual Studio Projects\test\bin\Debug\dcli.dll". Do you know why?
Thanks in advance.
|
|
|
|
|
Ugh. I think I remember reading/hearing something about this. I'm not sure I remember exactly, but I don't believe you can explicitly provide a path as you have. I think it looks for "filename.dll" in a set order of pathnames.
Jeremy Kimball
|
|
|
|
|