|
This is my latest code:
try
{
m_oZipFile = new ZipFile(new FileStream(l_szLocation, FileMode.Open, FileAccess.ReadWrite, FileShare.Read));
}
catch
{
MessageBox.Show("FILE IS ALREADY OPEN", "READ ACCESS ONLY", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
m_oZipFile = new ZipFile(new FileStream(l_szLocation, FileMode.Open, FileAccess.Read, FileShare.Read));
}
I am getting the following error:
The process cannot access the file 'C:\EncryptionTesting\DecryptInput\test-dispatch.config.rld' because it is being used by another process.
|
|
|
|
|
Your problem is probably in the second FileStream. FileShare.Read in the catch means that the first file stream (in the try) can have read access only.
<br />
catch<br />
{<br />
MessageBox.Show("FILE IS ALREADY OPEN", "READ ACCESS ONLY", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);<br />
m_oZipFile = new ZipFile(new FileStream(l_szLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));<br />
}<br />
|
|
|
|
|
Hi,every one,I have a problem in C# about calling dll writed by c, like the following,I can't get the correct answer.who can tell me why?
Here is the code about the dll's header file:
void _stdcall chuck(string);
void _stdcall wolfhome(string);
void _stdcall wolfsist(string);
void mein(char* directx11);
char* deme();
void inshuxiyin(char* directx11);
char* deshuxiyin();
char* _stdcall deshumil();
char* _stdcall shuredel();
char* _stdcall getDecryptStr();
void _stdcall wolfhomeseal(string);
void _stdcall wolfsistseal(string);
Here is the code about c#
[DllImport("bear.dll",CallingConvention=CallingConvention.StdCall)]
public static extern void chuck(string s);
[DllImport("bear.dll",CallingConvention=CallingConvention.StdCall)]
public static extern void wolfhomeseal(string s);
[DllImport("bear.dll")]
public static extern void wolfsistseal([MarshalAs(UnmanagedType.LPStr)] string s);
[DllImport("bear.dll",CallingConvention=CallingConvention.StdCall)]
public static extern string deshumil();
[DllImport("bear.dll")]
public static extern string shuredel();
[DllImport("bear.dll")]
public static extern string getDecryptStr();
These are the methods I have tried,but none works fine,what problem exitsts?
Waiting for your answer,thanks!
|
|
|
|
|
|
Hi,
anuo06 wrote: none works fine
that is very specific !! is there anything that works ? works sometimes ?
how does it fail ? what crash, error, exception occurs ?
several suggestions:
1. apply CallingConvention to each prototype; you did only some
2. are you sure you need StdCall; it could also be Cdecl !
3. if any functions are available that take and return integers, get them
to work first, then solve the string problem.
4. if you own the C source, add logging code to both sides, and log every
call with arguments and return values.
|
|
|
|
|
Thank you for your answer first,but what I whrited is that I used several methods to achieve the result (this is the code I tried to find a good one, not the real code, in the real code ,I will use the uniform format), and all these dosen't work correctly.
When I use integer type , it works fine, just string and char*, when they convert to c# data type, I can't get the correct result.
A more stranger thing is that when I transmit parameter use "public static extern void chuck(string s);", such as the string "hello", it will get the input string "hello", it is correct!but when I use "public static extern void wolfhomeseal(string s);", and transmit a same (or others) string ,it will get a input as "?", always "?",what is that means?
All the problems are occured in type "string" and "char *" in c, the type "int" I tested is no problem.
|
|
|
|
|
OK,
consuming a C# string in C/C++ is somewhat easier than producing a
C# string in C/C++ code.
and there is the matter of 8bit ANSI versus 16bit Unicode and maybe other
encodings.
Normal steps are:
a string input (a char* in C) should be declared string in C#
a string return value should be declared string in C# (even it is char* in C)
a string output (as a parameter) should be declared a StringBuilder in C#
(and it must have sufficient Capacity !)
I suggest you pick one case and publish actual code for it, both the C# side
and the C/C++ side, plus you mention exact symptoms (show the first few characters
or bytes in hex on both sides of the P/Invoke border.
|
|
|
|
|
Hi,here is the c code of the two function:
void chuck(string s)
{
printf("string into chuck is:\n%s\n",s);
char d5[64] = {0};
int d2[64] = {0};
for(int i=0;i<64;i++)
{
d2[i] = (s[i>>3]>>(i&7)) & 1;
}
for(int j=0;j<8;j++)
for(int k=0;k<8;k++)
d5[8*j+k] = huwuwuan(d2[8*(j+1)-(k+1)]);
char a_1[56] = {0};
for(int k=0;k<56;k++)
{
a_1[k] = d5[d1[k]-1];
}
jiankzi(a_1);
}
void _stdcall wolfhome(string f3)
{
printf("string into wolfhome start is:%s\n",f3);
int strin = f3.length();
if(strin == 8)
{
athome(f3);
memcpy(mill,taime,16);
mill[16] = '\0';
}
else if(strin < 8)
{
f3 = feel(f3);
athome(f3);
memcpy(mill,taime,16);
mill[16] = '\0';
}
else if(strin > 8)
{
int strI = ceil(strin/8.0);
int strI3 = strin % 8;
string iS12;
for(int i=0;i
|
|
|
|
|
anuo06 wrote: void chuck(string s)
{
}
anuo06 wrote: void _stdcall wolfhome(string f3)
{
int strin = f3.length();
}
First of all this is not C code, it may be C++ code.
Is this code inside a .cpp file ?
Second an apparant difference between both functions is the absence/presence
of the _stdcall keyword.
You clearly have a problem with your calling conventions.
My advice is simple: choose an appropriate calling convention and
make it explicit EVERYWHERE, i.e. add CallingConvention=... to every C# prototype,
and a matching _stdcall/_Cdecl/whatever to every C/C++ function you plan to call
thru P/Invoke. Dont rely on defaults, they are rather complex, and may change
when you change compiler settings.
Which one (stdcall/Cdecl) you choose depends on circumstances. Read the documentation!
|
|
|
|
|
If I use c code like these,what is the correct calling code in C#?
void chuck(string);
char* deshumil();
The type "string" and the type "char*" are the main problems.
|
|
|
|
|
If I use c code like these,what is the correct calling code in C#?
void chuck(string);
char* deshumil();
The type "string" and the type "char*" are the main problems.
|
|
|
|
|
Hi all,
Here is the background to this issue. I am writing a C# program that is to be a self-guided training software. I have a sneaking suspicion that I'm not doing it properly.
Currently, I have several (10+) form screens. As the user goes through each form, there is a next button, which:
(1) Creates a new instance of next form (form2) & passes some parameters to form2
(2) from2.show
(3) this.dispose
I'm now running into a problem where there are certain cases where the user cancels out or needs to go back several forms (i.e. on form5, need to load form1) and an error occurs becuase I have disposed the form.
What is the proper way to do this? I think there are two other options that may be more correct, but I'm not sure which is right:
(a) Have all the controls for all the forms on one form. Pressing next causes a set of form controls to be visible and hides the controls not currently used. i.e:
* Main form has 4 sub-forms, each form containing a button form1, form2, form3 & form4.
* Initially form1 button is shown, all others hidden.
* Press next, and form1 button is hidden, form2 button is shown, all others hidden
* etc.
I think this may get cumbersome and messy.
(b) Have a main form and it'll contain child forms
(c) Have a main form and use it as a container for MDI
Any suggestions? Advice? Please correct me if I'm totally off track. Thank you so much!
~~~~~~~~~~~~
Over-worked, under-paid, glorified "secretary"
|
|
|
|
|
Hi,
there are many ways to achieve this.
Putting everything on one form and modify the Visible properties is NOT good.
And I would not put all the forms on a master form either.
Creating separate forms once, and showing/hiding them is one way.
This means you do not Close() any form, instead you Hide() it until you wamt to
Show() it again.
Creating separate forms once or more is also OK: when moving backwards in
the course, simply create a new instance of the Form and Show() it; when you dont
want to see it anymore, Close() it.
Hope this helps.
|
|
|
|
|
Hi Luc,
Thanks! That helps alot. Although, now I have another question (based on your suggestions). First, there should only ever be one instance of each form.
So if I have a button on form1 (btnNext) that opens form2, how do I go from form2 back to form1?
I can figure going forward:
form2 myForm2 = new form2();
myForm2.show(); //show form 2
this.hide(); //hide form1
But going back, how do I reference the form1 that's hidden in the form2 code? Is it as simple as:
myForm1.show();
this.close();
I would think that myForm1 would not be in the scope of the form2 class. Thanks again!
~~~~~~~~~~~~
Over-worked, under-paid, glorified "secretary"
|
|
|
|
|
Skcheng wrote: myForm1.show();
this.close();
Almost; it is:
myForm1.Show();
this.Hide();
Dont Close() forms you still want to be available for possible later use;
use Hide() instead.
|
|
|
|
|
Thanks again! Can't wait to get home to try this out.
~~~~~~~~~~~~
Over-worked, under-paid, glorified "secretary"
|
|
|
|
|
|
how do you attach a serial command to a button in c#?
i have a lutron rs232 controller for my lights, and i want to know how you attach an on/off command to it.
this is what i have
private void button1_Click(object sender, EventArgs e)
{
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
port.Write("SDL,1,0") //just an example, "Set Dimmer Level, zone 1, 0 for off"
port.Close();
}
now the (SDL,1,0) i've put a "/r/n" as well, but i get no response.
i read that serial article for c#, but i'm confused to how you would use it. can someone n00b it up for me?
-- modified at 13:32 Tuesday 19th June, 2007
|
|
|
|
|
|
Hi,
Please see the function below.
When this function is called, it successfully finds the accurate value and assigns it to intFinalBaseIndexID i.e. return intFinalBaseIndexID; .
after the line i.e. return intFinalBaseIndexID; I am not sure why the function FinalBaseIndexID(dsCurves, intBaseIndexID, intBaseParentIndexID);//find the next Base... is called again? and so the function FinalBaseIndexID returns 0
Thanks
static int FinalBaseIndexID(DataSet dsCurves, int intIndexID, int intParentIndexID)
{
int intFinalBaseIndexID = 0;
//find the Base Index...
for (int intCurve = 0; intCurve < dsCurves.Tables.Count; intCurve++)
{
int intBaseIndexID = int.Parse(dsCurves.Tables[intCurve].Rows[0]["IndexID"].ToString());
int intBaseParentIndexID = int.Parse(dsCurves.Tables[intCurve].Rows[0]["ParentIndexID"].ToString());
if (intBaseIndexID == intParentIndexID)
{ //Base...
if (intBaseParentIndexID > 0)
{
FinalBaseIndexID(dsCurves, intBaseIndexID, intBaseParentIndexID);//find the next Base...
}
else
{
//Final Base...
intFinalBaseIndexID = intBaseIndexID;
}
}
if (intFinalBaseIndexID > 0)
{
break;
}
}
return intFinalBaseIndexID;
}
|
|
|
|
|
when you make the recursive call
FinalBaseIndexID(dsCurves, intBaseIndexID, intBaseParentIndexID);
you aren't storing the return value anywhere. Should it be
intFinalBaseIndexID = FinalBaseIndexID(dsCurves, intBaseIndexID, intBaseParentIndexID);
?
|
|
|
|
|
Is there an intrinsic way I can have a collection that has a separate dictionary key and a sort key? E.g. I want to store a collection of phone numbers, keyed by a TelNumberId int field, but sorted by a TelNumber string field.
|
|
|
|
|
If your dealing with classes (as aposed to structs) then I'd probably go for having 2 collections, one dictionary to act as the index and one List sorted in the order you want.
Each of these collections will only store the reference to the object so it's not like you'll be duplicating data.
|
|
|
|
|
Hi below is the code for conversion to grey scale image.
But i am getting exception at first line of the GrayScale function that
Object reference not set to an instance of an object.
I am using VS2005.
Any help would be appreciated....
Thanks
<br />
<br />
private Bitmap b;<br />
<br />
private void GrayScaleMenuItem_Click(object sender, System.EventArgs e)<br />
{<br />
if(GrayScale(b))<br />
this.Invalidate();<br />
}<br />
<br />
<br />
public static bool GrayScale(Bitmap b)<br />
{<br />
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);<br />
<br />
int stride = bmData.Stride;<br />
System.IntPtr Scan0 = bmData.Scan0;<br />
<br />
unsafe<br />
{<br />
byte * p = (byte *)(void *)Scan0;<br />
<br />
int nOffset = stride - b.Width*3;<br />
<br />
byte red, green, blue;<br />
<br />
for(int y=0;y<b.Height;++y)<br />
{<br />
for(int x=0; x < b.Width; ++x )<br />
{<br />
blue = p[0];<br />
green = p[1];<br />
red = p[2];<br />
<br />
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);<br />
<br />
p += 3;<br />
}<br />
p += nOffset;<br />
}<br />
}<br />
<br />
b.UnlockBits(bmData);<br />
<br />
return true;<br />
}
|
|
|
|
|
It seems that variable b is not initialized and its value is null
|
|
|
|
|