|
Yes you can, actually, but you need to have a class factory which creates an instance of the class (which gives you the IntPtr ) that you pass as the first parameter of a P/Invoke method using CallingConvention.ThisCall .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You can actually P/Invoke class methods so long as you have a class factory to create an instance of that class. The problem here - like the exception text reads - is that your callback function uses a different calling convention. To be honest, I'm not really sure how you're going to get around this. I've never had to deal with callbacks in classes before (just calling methods on exported native classes).
The StdCall shouldn't work since you need to push the class instance's address in the ECX register (which the first parameter in a ThisCall method is used for). Thus, you wouldn't really be calling a method but a function, treating it as if it were static.
I did find this[^], however, which I hope proves helpful.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
You can actually P/Invoke class methods so long as you have a class factory to create an instance of that class.
Is there any link that explains this? I cannot change the calling convention as the method exported is a member of the class and I need to pass the instance ptr of the class as the first argument to the method, to call the class methods. Is there any other way to call the class methods from the DLL?
Will it help, if I change the callback method declaration in the C++ Dll to something else? like,
bool mapImage(UINT16 SectorSize,
bool (* __stdcall CallbackFunctionAddress)(UINT32 SectorToBeRead, UINT8 * SetToSectorData, bool IsLiteral));
To call the methods from the exported class, I have added two methods to the class: 1. To return the instance ptr of the class to the client and 2. To remove the instance of the class. Is this the correct way?
Pls help...
Vini
|
|
|
|
|
|
Tnx. I have seen that previous post of yours before. I am able to call the exported class methods. The issue with callback function, I think I have it right now, is by modifying the C++ DLL source. Added the __stdcall to the Callback method declaration. Is this okay?
bool mapImage(UINT16 iSectorSize, bool (__stdcall * pCallback)(UINT32, UINT8 *, bool))
Tnx for the help.
Vini
|
|
|
|
|
i am doing my finalyear project in remote desktop sharing,i transfer the image to other machine with the help of socket classes tcplistener and tcp client.i transfer image with the help of System.IO.MemoryStream and convert this to bytes with method ToArray() of System.IO.MemoryStream. on receving side bytes are read and written to System.IO.MemoryStream then transfer this stream to bitmap image (Bitmap class instance)by method Bitmap.FromStream(ImageMemStream);ImageMemStream is instance of System.IO.MemoryStream.After some random and long interval of time i got an exception System.drawing.dll on line
Bitmap.FromStream(ImageMemStream);
PLease help me about this.
mughalali
|
|
|
|
|
How are we supposed to help you if you don't tell us what exception you're getting and what the exception message states? That like telling a doctor you have a paint but not telling him where.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Gurus! I got some question for you...
In my C# WinForm application I generate an html page. I created a new file with .htm extention through filestream and wrote some text to it. The question is how can I launch this file from C#? I mean I need to repeat such effect if I just open the folder containing this html file and double click on it in explorer, I need to make in in C#.
www.xedom.com
|
|
|
|
|
try
Process.Start("IExplore.exe", "<yourfile.html>");
Cheers,
Kannan
|
|
|
|
|
System.Diagnostics.Process.Start( htmlFileName ); Process.Start uses the ShellExecuteEx API by default, so it will launch the user's default browser.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Thanx a lot. I'll try this without fall. Also I want to ask you some more - I want to send a request to the server's php page, through my WinForm application, I use code like this
Uri u = new Uri(@"http://www.umc.ua/misc/sendsms.php");
WebRequest req = WebRequest.Create(u);
req.ContentType = "application/x-www-formurlencoded";
req.Method = "POST";
req.Timeout = 300000;
req.Headers.Add("network1","UMC");
req.Headers.Add("Phone1","5918658");
req.Headers.Add("network2","UMC");
req.Headers.Add("Phone2","");
req.Headers.Add("network3","UMC");
req.Headers.Add("Phone3","");
req.Headers.Add("Message","HellofromCSharp");
req.Headers.Add("sender_name","sn");
WebResponse res =req.GetResponse();
text.Text = req.Headers.ToString();
StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.UTF8);
string str = sr.ReadToEnd().ToString();
RES_text.Text = str;
res.Close();
But my application fails with Timeout expired bug. So could you tell me were the problems is? How can I send data to php page in C# code using POST method? Thanx.
www.xedom.com
|
|
|
|
|
Why are you adding headers? I take it that's what you want to post to the PHP CGI? See the HttpWebRequest.GetRequestStream[^] method documentation in the .NET Framework SDK for an example.
Basically, you get the request stream before you get the HTTP response and write your &-delimited parameters and values (delimited by =) to the response stream using an Encoding that matches up with the HttpWebRequest.ContentType (application/x-www-form-urlencoded uses ASCII encoding, so use Encoding.ASCII or ASCIIEncoding (the same thing, just different ways of getting an instance of it). After you're finished, you flush and close the request stream and then get your response. The link above has a more detailed example.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ok, thnank you Heath. But I have done following things and still no effect:
string post_data;
post_data = "?network1=UMC&Phone1=5918658&Message=HellofromCSharp&sender_name=sn";
string page = "@http://www.umc.ua/misc/sendsms.php"+post_data;
WebRequest req = WebRequest.Create(page);
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.ASCII);
string str = sr.ReadToEnd().ToString();
RES_text.Text = str;
res.Close();
www.xedom.com
|
|
|
|
|
did you look at the example? When POST'ing data you must get the request stream - not the response stream - and write your post_data (without the preceeding "?") and write it to the request stream, then close it and get your response. This is shown in the example at the bottom of the page I linked.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
still not works..;( I use following code
string page = "http://www.umc.ua/misc/sendsms.php";
WebRequest myHttpWebRequest = WebRequest.Create(page);
string postData="network1=UMC&Phone1=5918658&Message=HellofromCSharp&sender_name=sn";
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);
// Set the content type of the data being posted.
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
// Set the content length of the string being posted.
myHttpWebRequest.ContentLength=postData.Length;
Stream newStream=myHttpWebRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);
RES_text.Text = "The value of 'ContentLength' property after sending the data is {0}" + myHttpWebRequest.ContentLength.ToString();
// Close the Stream object.
newStream.Close();
And when I launch my app;ication and click button send, following exception occurs
************** Exception Text **************
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
www.xedom.com
|
|
|
|
|
even if I do this:
HttpWebRequest myHttpWebRequest = (HttpWebRequest )WebRequest.Create(@"http://www.umc.ua/misc/sendsms.php");
string postData="?network1=UMC&Phone1=5918658&Message=HellofromCSharp&sender_name=sn";
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);
// Set the content type of the data being posted.
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
// Set the content length of the string being posted.
myHttpWebRequest.ContentLength=postData.Length;
Stream newStream=myHttpWebRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);
RES_text.Text = "The value of 'ContentLength' property after sending the data is {0}" + myHttpWebRequest.ContentLength.ToString();
// Close the Stream object.
newStream.Close();
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
at System.Net.HttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
at System.Net.HttpWebRequest.GetRequestStream()
at sm.Form1.heath_Click(Object sender, EventArgs e) in c:\test projects\sm\form1.cs:line 281
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
sm
Assembly Version: 1.0.1578.33862
Win32 Version: 1.0.1578.33862
CodeBase: file:///C:/Test%20Projects/sm/bin/Debug/sm.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitdebugging="true">
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.
www.xedom.com
|
|
|
|
|
You could do some parsing. If it weren't for the pesky enclosing parentheses, your job would be a lot easier. I'm assuming the same structure isn't entered every time.
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
ArrayList arr = new ArrayList();
ArrayList newArr = new ArrayList();
this.tbFormula.Text = this.tbFormula.Text.Replace(" ", "");
foreach (string accID in this.tbFormula.Text.Split('+', '-', '*', '/'))
{
string num = accID;
arr.Add(num);
}
for (int i = 0; i < arr.Count; i++)
{
num = arr[i].ToString();
if (num.Substring(0, 1).Equals("("))
newArr.Add(num.Remove(0, 1).ToString());
else if (num.Substring(num.Length - 2, 2).Equals("))"))
newArr.Add(num.Remove(num.Length - 1, 1).ToString());
else
newArr.Add(num.ToString());
}
for (int i = 0; i < newArr.Count; i++)
{
string num1 = newArr[i].ToString();
MessageBox.Show(num1.ToString());
}
Mr Duc Linh Nguyen
|
|
|
|
|
You can make this sort of approach a lot speedier. Notice that you didn't even give me a complete list of operators when you posted your question, but I'm glad that you seem to be happy...
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
Or you can use String.Split(Char[], Int32) method. Which you pass +/*- as first paramter.
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Regex re = new Regex(@"(?<token>[A-Z]{2,4}\(\s*\d+\s*,\s*\d+\s*\))");
|
|
|
|
|
I have the following string format(s) that I am trying to parse.
1-1-1
1-1-2
1-1-10
2-222-1
22-2-31
or
xx-xxx-xx
from which I would like to place xx into an ArrayList, xxx into an ArrayList2 and xx into an ArrayList3. The '-' are delimiters.
Any suggestion would deeply help.
Sincerely,
Tony D. Abel
Have a great coding day.
Regards,
Tony D. Abel
|
|
|
|
|
Simplest for you, although not fastest to execute, would be
char[] delimiters = new char[] {'-'};
string s;
string[] substrings;
substrings = s.Split(delimiters);
Regards,
Jeff Varszegi
|
|
|
|
|
Good day Jeff...
Again thanks for your input. This is what I came up with:
Int32 loadArrays = 0;
char[] delimiters = new char[] {'-'};
string[] substringsBookChapVerID = null;
string strBookChapVerID;
ArrayList chapterList = new ArrayList();
ArrayList verseList = new ArrayList();
///
/// Get the columns primary ID string from the row
///
foreach ( DataRow bibleRow in bibleDataset.Tables[BibleColumnName].Rows )
{
strBookChapVerID = (string) bibleRow[BibleColumnName];
substringsBookChapVerID = strBookChapVerID.Split( delimiters );
///
/// Get the book, chapter and verses substrings
///
foreach ( string number in substringsBookChapVerID )
{
///
/// Save the book, chapter and verses substrings for sorting
///
switch ( loadArrays )
{
case 0:
bookNumber = number;
loadArrays++;
break;
case 1:
if ( number == "0" )
{
loadArrays++;
break;
}
if ( chapterList.Count == 0 )
{ chapterList.Add( number );
loadArrays++;
break;
}
else if ( chapterList.IndexOf( number ) <= -1 )
{
chapterList.Add( number ); loadArrays++;
break;
}
loadArrays++;
break;
case 2:
if ( number.TrimEnd( ' ' ) == "0" )
{
loadArrays = 0;
break;
}
verseList.Add( number.TrimEnd( ' ' ) );
loadArrays = 0;
break;
} // end switch loadArrays
} // end number
} // end bibleRow
I hope you may be able to use this snippet in the future for some project
Have a great coding day.
Regards,
Tony D. Abel
|
|
|
|
|
I got string that was entered by user like this:
"(DC(112, 113) + DN(101, 102)) / CLDC(122, 123)......".
Please help me how to get exactly the "DC(112, 113) and DN(101, 102) and so on" string.
Thanks
Mr Duc Linh Nguyen
|
|
|
|