|
A similar problem exists when using Environment.CurrentDirectory as the current directory is not necessarily the folder in which your application resides.
You can test this yourself by creating a console application containing the following code:
public static void Main(string[] args)
{
Console.WriteLine("Current Directory: " + Environment.CurrentDirectory.ToString() );
Console.WriteLine("Press any key to continue...");
Console.ReadKey( true );
}
Now if you run the code, it does as you would expect.
But if you open your applications folder in explorer then right-click and 'create a shortcut', you can edit the properties of the shortcut and change the 'Start in' parameter to 'C:\'.
When you then run the application using the shortcut, the current directory is set to 'C:\' and not that of your application.
Please see my previous post for the correct method.
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
View my Blog
|
|
|
|
|
Hi all,
I've asked this question before, but didn't receive a response, so I'm posting it again but with more info which should HOPEFULLY help someone tell me what I'm doing wrong.
I am trying to use a DLL that modifies a stream of bytes (basically it's an encryption/decryption DLL). This DLL comes from someone else and I have to use it "as is" (no way to modify it).
The DLL reads and writes to a byte array (reads encrypted data, writes decrypted data), and this is giving me the error "Attempted to read or write protected memory."
I think the problem is quite simply that I don't understand how C# handles this kind of thing, so was wondering if I could get a little help.
The code goes something like this:
[DllImport("ABCdecrypt.dll", EntryPoint = "ABCdecrypt", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
unsafe public static extern bool ABCdecrypt([MarshalAs(UnmanagedType.I4)] int type, byte* data, int datalength);
private void buttonGo_Click(object sender, EventArgs e)
{
byte[] yData = File.ReadAllBytes(textBoxInput.Text);
unsafe
{
fixed (byte* pBuf = &yData[0])
{
ABCdecrypt(100, pBuf, yData.Length);
File.WriteAllBytes(textBoxOutput.Text, yData);
}
}
MessageBox.Show("Done");
}
If it helps, the sample code I was given for C++ looks like:
BYTE data[] = {'1','2','3','4','5','6','7','8'};
int type = 100;
int bufsize = (sizeof(data)+8) & ~7;
PBYTE pBuf = new BYTE[bufsize];
memcpy(pBuf,(LPCSTR)data, sizeof(data));
ABCdecrypt(type, pBuf, &bufsize);
delete pBuf;
And the reference document says the call should be:
<br />
ABCDecrypt(int type, LPBYTE data, int* datalength);<br />
If I'm not being clear, please ask me to clarify. Essentially, all I want to do is use the library to decrypt the file referred to by textBoxInput.Text and save it out as the file referred to by textBoxOutput.Text
|
|
|
|
|
Hi YttriumOxide,
Hi didn't notice that you've made a newer post of the same question so I've answered on the previous one. However, I notice one thing in this post that seems to differ from your previous one and which may well be the source of your problem.
The final parameter of ABCDecrypt is int* datalength but when you import the function from the DLL you specify the parameter as int datalength . Then when you call it you pass yData.Length which is an int, not an int*. Also, I see you import the second parameter ae byte* data . I'm not sure if makes any difference in C# but it might just make better sense to import it as byte[] data .
So I would think that you should import the function from the DLL as:
unsafe public static extern bool ABCdecrypt([MarshalAs(UnmanagedType.I4)] int type, byte[] data, ref int datalength);
And then, when you call it, it should look something like:
ABCdecrypt(100, yData, yData.Length);
I assume this function takes a byte array (data ) of length datalength and decrpyts it according to some algorithm specified by type . It then writes the result into data again and updates the datalength value also. This would suggest to me that the returned byte array may be of a different size than the input byte array. If this is the case, you need to make sure that the size of yData is big enough to hold the returned byte array.
If you know that the returned byte array will never be bigger than 256 bytes, you could do the following:
byte[] myBuffer = new byte[256];<br />
byte[] yData = File.ReadAllBytes(textBoxInput.Text);<br />
int DataLen = yData.Length;<br />
<br />
Array.Copy(yData, 0, myBuffer, 0, DataLen);<br />
ABCdecrypt(100, myBuffer, ref DataLen);
At this point, myBuffer should contain DataLen bytes which represents the returned byte array.
There may very well be more elegant sollutions but I am not very strong in C#. Most of what I wrote here came from the C++ programmer in me;)
|
|
|
|
|
Hi Dewald,
Okay, now I REALLY feel like an idiot for missing that one - I guess it's just one of those things that it helps when another pair of eyes looks at it.
I haven't got a chance to try it out now, but I'm 99% certain that you've just solved my problem, so thankyou very much
As a note, I generally code C++ as well (albeit, not brilliantly yet), as I'm mostly a Linux coder and I'm just forced in to C# on Windows at work.
|
|
|
|
|
Is there a way I can place a user control onto a form in designer mode without having to have it go into the GAC? The tutorials out there seem to indicate this is the only way to get it into your toolbox.
Regards,
James
Did I post well? Rate it! Did I post badly? Rate that too!
|
|
|
|
|
If I understand you correctly, this may solve your problem. This is how I did it on a project I'm currently working on.
Show your toolbox in VS. Right click on the toolbox, and select the Choose Items... menu option.
A dialog now shows that is titled "Choose Toolbox Items". Click browse, find the DLL or EXE your user control is in, and click OK.
The list that is under the .NET Framework Components tab should now list all User Controls that DLL or EXE contained. Make sure that the checkbox next to the user control you want to put on your form is checked, then click OK. The "Choose Toolbox Items" dialog goes away, and your User Control is added to the currently selected tab in your toolbox (doesn't look like a tab, but that is what the context menu calls them)
You should now be able to drop that user control on your form.
BTW, I use VS2005, so I'm not sure if its the same for VS2003.
HTH.
|
|
|
|
|
hi
i want to hide one or more row of datagridview that bind to dataset, but the following error shown me :
Row associated with the currency manager's position cannot be made invisible
how to solve this problem ?
|
|
|
|
|
How can |I get a lighter tone of a particular color. Like if I have a value of color in rgb wht changes I have to make to it to make the same color with the lighter tone......
|
|
|
|
|
Take a look at Christian Graus articles on C# Image Processing with GDI+
that should help.
led mike
|
|
|
|
|
R: 0, G: 0, B: 0 = Black and R: 255, G: 255, B: 255 = White, so to make it a lighter shade, you'd just increase its RGB value equally.
So, an example -
If you have a blue (R: 0, G: 45, B: 255) you'd increase the RGB value by 80 to get a much lighter blue, and since the Blue is already max'd out at 255 you'd only increase the RG values - so you'd end up with a light blue (R: 80, G: 125, B: 255).
Hope that helps
|
|
|
|
|
I believe that you can use methods in the System.Windows.Forms.ControlPaint class. Use Light(Color c) or LightLight(Color c) to get lighter variations of the specified color.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
How would this be accomplished easily?
I want to remove all rows in a DataGridView in the DataGridView.SelectedRows property.
Then I want to put them in another DataGridView's DataSource.
The DataSource for both DataGridView objects is a DataTable.
Thanks in advance,
Pualee
|
|
|
|
|
Mainatain two datatables and add the new rows/records to your second datatable which should be the datasource for the second datagrid
Gautham
|
|
|
|
|
Hello everyone,
I have a ListView contorl which has 4 columns. I was wondering if it is possible to make one column invisiable to the user?
Thank you very much and have a nice day.
Khoramdin
|
|
|
|
|
Did you try setting it's width to 0 (Zero)?
led mike
|
|
|
|
|
Hello Mike,
I can set it to zero but if I am not mistaken user can open it. Unless I am missing a point here!
Thank you for your help.
Khoramdin
|
|
|
|
|
Ok but if you don't want the user to see it, why have it there at all?
led mike
|
|
|
|
|
Hi guys,
Need some help please
I have a file on our webserver a .dat file that has a HTTP:// path. I am trying to open,read and write to the file from a C# windows application.
Is this possible? Please advice how to go about this.
Thanks in advance.
sasa
|
|
|
|
|
You can read it by downloading the file using HttpWebRequest, but that's about it. HTTP servers usually don't let you write anything back or open files for streamed read/write. Think about the gynormous security problem that introduces.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Thank you Dave for your suggestion. I used the webclient class and it worked. I am reading it, I wish I could write to it though.
Thank you for your suggestion again.
sasa
|
|
|
|
|
StreamReader would take physical path. You can use HttpWebRequest and use the Stream in Response to manage the file.
|
|
|
|
|
Here is the code I'm having trouble with:
Type oBB = Type.GetTypeFromProgID("MyNamespace.MyClass");
Object o = Activator.CreateInstance(oBB);
Object oResult = oBB.InvokeMember("BuildBook", System.Reflection.BindingFlags.InvokeMethod, null, o, new object[]
{ (int)Job.JobID, @"\\folder\in\" + Job.FinalFile.ToString(), Convert.ToBoolean(Job.Contribidx) });
Type oRes = oResult.GetType();
String sName = (String)oRes.InvokeMember("cName", BindingFlags.GetProperty, null, o, null);
The problem is that when I check sName, it gives me the cName property of MyClass rather than the cName property of the object returned by BuildBook. Does anyone see any obvious mistakes in my code?
|
|
|
|
|
Shouldn't the statement:
String sName = (String)oRes.InvokeMember("cName", BindingFlags.GetProperty, null, o, null);
be
String sName = (String)oRes.InvokeMember("cName", BindingFlags.GetProperty, null, oResult, null);
-Phil
|
|
|
|
|
You da man Phil...you da man!
|
|
|
|
|
In the constructor of my main form, I do some checking.
If it doesn't pass the check, I give a MessageBox then I
want to quit the program. As I have the code now, I get
an ObjectDisposeException. What is the proper way to
quit the program?
public Form1()
{
InitializeComponents();
if(.......){
....
....
this.Close();
}
}
|
|
|
|
|