|
Just make one:
int[] MyArray = ...<br />
IntPtr MyPointer = IntPtr.Zero;
Marshal.Copy(MyArray, 0, MyPointer, MyArray.Length);
(Note: Don't confuse the marshal-call with the overload Marshal.Copy(IntPtr, int[], int, int) .)
|
|
|
|
|
Nope, Marshal.Copy throws a NullReference exception when passing
a null (0) IntPtr object.
So for now, the "mystical" solution works the best
|
|
|
|
|
You're right, my mistake. I wrongly assumed that the Copy method returns a pointer to the array. On second thought this isn't what Copy does at all. Instead you have to allocate some space yourself using Marshal.AllocHGlobal with the appropriate number of bytes. This gives you a valid IntPtr.
int Nr = ...;
int[] MyArray = new int[Nr];<br />
IntPtr MyPtr = Marshal.AllocHGlobal(Nr * sizeof(int))
Then call SendMessage with MyPtr.
Next use the Copy(IntPtr MyPtr, int[] MyArray, 0, MyArray.Length) method.
Finally use Marshal.FreeHAlloc.
|
|
|
|
|
Well, this time works but the values returned are messed.
But, you know, this does work fine:
_columnOrder = new int[this.Columns.Count];<br />
int size = 0;<br />
<br />
IntPtr pHeader = SendMessage(this.Handle, (int) Win32.Consts.LVM_GETHEADER, 0, ref size);<br />
<br />
SendMessage(pHeader, (int) Win32.Consts.HDM_GETORDERARRAY, _columnOrder.Length, ref _columnOrder[0]);
See the ref _columnOrder[0] in the second SendMessage call
|
|
|
|
|
OK, good luck with your project.
|
|
|
|
|
Im trying to do this:
ResourceManager rm = new ResourceManager("GameRPG", this.GetType().Assembly);<br />
System.IO.FileStream fs = (FileStream)rm.GetObject("outside");
So that i can save the file "outside" (which is stored in the resources) to a folder so that it can be used.
But i just get the message "Specified cast is not valid" (The problem is with the file stream by the way, not the resource manager)
How can i get this to work?
|
|
|
|
|
I assume you have already tried splitting your second line up to
object TheObject = rm.GetObject("outside");<br />
System.IO.FileStream fs = (FileStream)TheObject;
and setting a breakpoint on the first line. Start your app and look in the debug window what type TheObject really is. You'll probably know then what went wrong, or at least get a good clue.
|
|
|
|
|
I have no debug window
Im using sharpdevelop
|
|
|
|
|
My mistake. Try adding
string TheObjectType = TheObject.GetType().ToString();
and writing this string to screen or file.
|
|
|
|
|
No debugger ? Good luck writing anything meaningful.
ResourceManager rm = new ResourceManager("GameRPG", this.GetType().Assembly);
if (rm == null)
MessageBox.Show(""rm is null"); // Obviously not the case, but anyhow....
object o = rm.GetObject("outside");
if ( o == null)
MessageBox.Show("o is null");
MessageBox.Show(o.GetType().ToString()); // Something like this, to find out the type of your object.
System.IO.FileStream fs = (FileStream)o; // This will still throw, but you will know why.
Also, if an object may not be of the type you expect, you can do this:
FileStream fs = rm.GetObject("outside") as FileStream;
if ( fs != null)
{
// it was a filestream
}
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi:
Did you check the contents of rm.GetObject("outside") in QuickWatch. I think it is getting evaluation to Null and since Null can not be cast to FileStream class, you are getting
System.InvalidCastException
Deepak Kumar Vasudevan
Personal Web: http://vdeepakkumar.netfirms.com/
I Blog At: http://deepak.blogdrive.com/
|
|
|
|
|
I'm not sure whether the SharpDevelop IDE has a QuickWatch. Anyway, I assumed that the original posted already checked whether the value retrieved by GetObject was not null, because you don't get an "invalid cast" exception when the object is null. In Visual Studio at least, maybe SharpDevelop reacts differently.
|
|
|
|
|
Well thanks guys, i'll try this tomorrow, and i'll download and install a debuger, especially after all the trouble i've had with this.
|
|
|
|
|
After using
<br />
FileStream fs = rm.GetObject("outside") as FileStream;<br />
I thought it had worked but it turns out that the filestream fs is still null.
Any thoughts, also the type of the object is a Byte[]
|
|
|
|
|
Does anyone know how to construct a vectorized (not a rasterized!, i.e. not a bitmap) metafile? I've tried the code below, but all I get a rasterized picture (I suspect it to be a PNG ).
Metafile MyMetafile = null;<br />
using (Graphics RefGraph = Graphics.FromHwnd(IntPtr.Zero))<br />
{<br />
IntPtr RefDC = RefGraph.GetHdc();<br />
MyMetafile = new Metafile(RefDC, new Rectangle(0, 0, MyWidth, MyHeight), <br />
MetafileFrameUnit.Pixel);<br />
RefGraph.ReleaseHdc(RefDC);<br />
}<br />
using (Graphics Graph = Graphics.FromImage(MyMetafile))<br />
{<br />
Brush Br = new SolidBrush(Color.Blue);<br />
Graph.FillRectangle(Br, 0, 0, MyWidth / 2, MyHeight / 2);<br />
}<br />
<br />
MyMetafile.Save(@"c:\temp\test.wmf", ImageFormat.Wmf);
I'm using a screen device context as a basis for the metafile since a reference DC is required when recording a metafile. Maybe that's what's wrong, because this screen DC is rasterized. But I don't know how to get a vectorized DC or if such a thing even exists. Or maybe I'm on the wrong track.
Any help on how to make a vectorized wmf or emf would be helpful.
Thanks.
|
|
|
|
|
Right, where do I begin? Well, I wouldn't call myself a windows programmer, that's for sure. I work writing embedded C, and needed to write some windows code to help test and productionise some products. So I chose C# and I'm using SharpDevelop as an IDE.
The application I'm writing sends and recieves commands over a serial link to my microcontroller. That's working fine, but I want to keep a log of what's been sent and recieved. Ideally both on the screen and in a file. I think I'll be able to manage the file stuff (since I've already worked out how to open and read a hex file), but I'm totally stumped on how to show the log. I want to be able to write/append text to a control such as:
<br />
log.Text += "Port Opened\n";<br />
log.Text += "Ping Sent\n";<br />
log.Text += "Port Closed\n";<br />
Giving me a straightforward box with the following in it.
<br />
Port Opened<br />
Ping Sent<br />
Port Closed<br />
It needs to be vertically scrollable to allow a user to look back at what's happened. I hope somebody can tell me how to so this (I hope) straightforward thing, which has got me stumped.
|
|
|
|
|
Dont't worry, it's not a big think.
- Create a TextBox with Multilne=true and Scrollbar eg.: called it log
- tahan when you want something to write to the textbox just write:
log.Text+="something";
- if you want to write a newline
log.Text+="\r\n";
If ou want to write the log to file (eg.: from the textbox):
<br />
System.IO.StreamWriter sw=new System.IO.StreamWriter("c:\\logfile.txt");<br />
foreach(string line in log.Text.Split("\r\n".ToCharArray()))<br />
{<br />
if(line.Trim().Length>0)<br />
{<br />
sw.WriteLine(line);<br />
}<br />
}<br />
sw.Close();<br />
|
|
|
|
|
I thought it would be easy, and that's great, thanks!
|
|
|
|
|
Hi,
I've written a windows service to parse files.It uses the FileSystemWatcher class in C# to sniff a folder for particular type of files. But when the files were being processed, i repeatedly get the error "The process cannot access the file "AAA" because it is being used by another process". To overcome this problem, i added the following. lines of code
public bool ParseEnvFile(string strFilePath)
{
try
{
using(FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
fs.Close();
m_nReadAttempts = 0;
}
}
catch(IOException ex)
{
if(m_nReadAttempts < 3)
{
Thread.Sleep(2000);
m_nReadAttempts++;
return ParseEnvFile(strFilePath);
}
else
{
throw ex;
}
}
.....
}
But even this did not solve the problem. The exception still kept coming, though less frequently.
So I was forced to add a Timer event(runs every 5 mins) that would pick up all the files that were left over because of the exception.
Now, my problem is this:
When a file gets created in the folder, sometimes both the OnFileCreated event and the timer event try to access the file at the same time. Within my
service, I'm required to open the file in write mode and update some contents. I find that at times the file that gets created ends up as a 0KB file (Note: when it initially got created the file had some content in it)!!!!
Any ideas on how to solve it?
Thanks for any help in resolving this problem.
|
|
|
|
|
Hi!
Perhaps you should check _which_ other program is accessing your files.
I've had a similar problem and it turned out that a virus scanner was the culprid.
One other thing: Do you really want to have recursion in your code? I'd say a do/while loop would be clearer, but that's just my opinion...
Regards,
mav
|
|
|
|
|
Hi mav..
Thanks for your reply. When i tried to debug the program when i first faced this problem I found that the FileSystemWatcher was only holding the file So when i tried google it, many others had also said the same. That is why the wait logic was introduced.
Yes i can do away with the recursion logic.
Regards,
G3
|
|
|
|
|
Example read the valure from first colmun, and build a conditional query with it.
|
|
|
|
|
I have no problem writing a winforms application and running it inside Internet Explorer using the <object> tag.
But how do I make the same app run inside Firefox?
// Per
|
|
|
|
|
Hi all
On my Windows application form I have a TreeView on the left and a number of textboxes on the right. The Treeview shows a list of users and the textboxes are for editing or adding new users. When I click on a user in the TreeView, the textbox fields are populated with the selected user's details. After I click the save button the TreeView is being reloaded to reflect the changed details of the user. How can I select that specific node in the TreeView programmatically so that it still shows that user as being selected?
Help on this or any suggestions on a better method would be appreciated.
Thank you.
kobus
|
|
|
|
|
set the SelectedNode property of your treeview
|
|
|
|