|
It is possible, but I'll explain in a direct response.
I just wanted to add that Dave is correct regarding the performance hit. Using Debug.WriteLine , for example, is a good idea. It is, however, removed when the DEBUG pre-proc symbol is not defined. In VS.NET, the default Debug build configuration defines both DEBUG and TRACE, while the Release build configuration defines only TRACE (the Trace class only appears when TRACE is defined).
This is done through the ConditionalAttribute and is a compiler features (compilers can choose to support it).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You can, but as Dave mentioned this is a serious performance hit, especially if you don't want to add those log lines (in which case using Debug.WriteLine or Trace.WriteLine is a better idea since you can enable them conditionally, though the call is made each time anyway).
The concept is known as AOP. Basically, your component resides in a different context, which requires that all parameters are marshalled across context boundaries. This can be very expensive, but gives you the flexibility to add aspect-oriented message handlers. There was an article on MSDN about this not so long ago (several others on others sites, too). See AOP: Aspect-Oriented Programming Enables Better Code Encapsulation and Reuse[^] in MSDN Magazine online.
Marc here on CP also has a great article covering AOP, Aspect Oriented Programming / Aspect Oriented Software Design[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Gurus! Please help me with little problem I got..
I do following thing to send data to php page through POST method:
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();
BUT when I click send buton which invokes this code, following exception occurs:
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:
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
|
|
|
|
|
As I mentioned before, drop the preceeding "?" in postData :
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://www.umc.ua/misc/sendsms.php");
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded";
string postData =
"network1=UMC&Phone1=5918658&Message=HellofromCSharp&sender_name=sn";
byte[] buffer = Encoding.ASCII.GetBytes(postData);
request.ContentLength = buffer.Length;
Stream s = request.GetRequestStream();
s.Write(buffer, 0, buffer.Length);
s.Flush();
s.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Great thanx! It works now. One more thing: data posted but my WinForm Application hangs after that...
www.xedom.com
|
|
|
|
|
Then you've got some code that isn't finishing execution. Step through your code and find out on which line it "hangs" (blocks). If you're making synchronous calls, execution of the code is done in the same thread that your control was created in (the UI thread), so nothing else happens (no response to user input, typically no painting, etc.) until that code finishes executing.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have commented last line of the code and all works fine for now:
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.umc.ua/misc/sendsms.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "network1=UMC&Phone1=5918658&Message=We did it! &sender_name=sn";
byte[] buffer = Encoding.ASCII.GetBytes(postData);
request.ContentLength = buffer.Length;
Stream s = request.GetRequestStream();
s.Write(buffer, 0, buffer.Length);
s.Flush();
s.Close();
//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//response.Close();
Could you explain me please for what I need this line-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
www.xedom.com
|
|
|
|
|
That's what submits the request and gets the response from the server. You can ignore the response if you like, but you really should check the HttpWebResponse.StatusCode to make sure the operation succeeded.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am looking into porting a COM C++ out of process server to C#. Been looking for an example of an out process server written in C#, can't seem to find any info in MSDN or anywhere else.
Any assistance appreciated.
Thanks in Advance.
Colin
|
|
|
|
|
See Nick's article, Creating a CCW for COM-enabled Non-.NET Applications[^] and follow the same things, paying attention to the note I prompted him to add about not using auto-generated class interfaces.
Then you register the executable as you would a DLL. Take a look at this sample:
using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyFile(@"C:\Path\To\KeyFile.snk")]
namespace Test
{
[Guid("0eea66bf-21bf-4c8c-8678-ad58106343a7")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITest
{
[DispId(0)]
DialogResult ShowDialog();
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("861e1098-7705-4686-945e-236a0ef439ef")]
[ProgId("Test.Test")]
public class Test : Form, ITest
{
static void Main()
{
Application.Run(new Test());
}
public Test()
{
SuspendLayout();
Text = "Out-of-Proc Server Test";
Label lbl = new Label();
Controls.Add(lbl);
lbl.Location = new Point(8, 8);
lbl.Text = "Hello, world!";
ResumeLayout();
}
}
} Compile it and run:
regasm.exe /tlb /codebase Test.exe Finally, I create a very simple JScript file that I ran with cscript.exe (Windows Scripting console host):
var test = new ActiveXObject("Test.Test");
test.ShowDialog(); The important thing is that your interfaces are dispatch interfaces (or dual) so that the script engine can find and invoke your members.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks for the reply, I looked at Nicks Article, following the same things and using the sample I could not get it to run out of process.
A better description of what I am trying to do might help:
I have built a COM Callable Wrapper (CCW) using Microsoft .NET & C#. The CCW uses attributes to provide a COM interface into a .NET component. This CCW can be created in-process using standard CoCreateInstance calls. However, it is necessary that this CCW runs out-of-process replacing an original COM component built using VC++ as a COM EXE service.
I have used the SDK supplied COMREG sample instead of REGASM to register the component with an AppID indicating that it should be run with a surrogate process – the system supplied surrogate DllHost.EXE.
The CCW can then be created out of process when CoCreateInstanceEx with a local COSERVERINFO address, ie, via DCOM. However, existing clients use standard CoCreateInstance to create the CCW; however standard CoCreateInstance causes the CCW to be created in-process instead of out of process.
Is their a mechanism to force the CCW to be created out-of-process (as if created using CoCreateInstanceEx) with the standard CoCreateInstance call.
I am investigating futher, any further you have to add would be greatly appreciated.
Regards
Colin
|
|
|
|
|
Yeah, I know what an out-of-proc server is. I've been working with COM since it was known as OLE (so to speak). Did you try compiling, registering, and executing the script file I wrote in my reply? It works perfectly fine. You compile it as an exe:
csc /t:exe Test.cs Follow the rest of the instructions and you'll get an out-of-proc server.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I followed your instructions to the letter, and I did not get an out of process server
|
|
|
|
|
What exactly do you mean that you don't get an out-of-process server? It works just fine. My forte' is interoperability in .NET and I do this all the time. The example does work, and if you need a typelib the /tlb switch on regasm.exe will generate one. If you make a script file like I provided, then execute the script with the Windows Scripting Host (cscript.exe for this example). Check the task manager and you'll see the application running. The ShowDialog method defined in the explicit class interface is just one method to show in this example. If you need access to anything else (like form properties, methods, or your own class's properties and methods) then you'll need to define those in the class interface as well, or at least another dispatch interface you could implement. The CLR will take care of the rest.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I created a script file, exactly as you provided. I get a dialog box with "Out-of-Proc Server Test" in the title bar and Hello world as expected. I have checked task manager and the application I see running is cscript.exe. Is this what you mean? What application do you mean? I was expecting test.exe(the out-ofproc server exe), which is not in task manager, maybe I am missing something here. My understanding of an out of process server is "A server, implemented as an .EXE application, which runs outside the process of its client", cscript being the client in this case. I apologise for the confusion, if I have caused any. I have done this in COM, however this is the first time I have tried to do it in .NET.
|
|
|
|
|
mscoree.dll - the CLR host - creates an AppDomain for the executable and loads the assembly, executing the main entry point and marshalling calls. This does not create a separate process as you've seen. I'm not even sure you could do this using pure .NET. You might have to create your own shim (like mscoree.dll) that runs as a process and marshals calls to your managed AppDomain. I remember seeing some code once for a custom host such as this (sorry, can't remember where - though you could try SourceForge) and it was by no means a simple task.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Have a look at my plugin manager article, someone mentioned that was similar to an out of process server.
|
|
|
|
|
Leppie,
Thanks for the reply, I had a look at your plugin manager article, tried to do the same in my situation and could not get it to run out of process.
A better description of what I am trying to do might help:
I have built a COM Callable Wrapper (CCW) using Microsoft .NET & C#. The CCW uses attributes to provide a COM interface into a .NET component. This CCW can be created in-process using standard CoCreateInstance calls. However, it is necessary that this CCW runs out-of-process replacing an original COM component built using VC++ as a COM EXE service.
I have used the SDK supplied COMREG sample instead of REGASM to register the component with an AppID indicating that it should be run with a surrogate process – the system supplied surrogate DllHost.EXE.
The CCW can then be created out of process when CoCreateInstanceEx with a local COSERVERINFO address, ie, via DCOM. However, existing clients use standard CoCreateInstance to create the CCW; however standard CoCreateInstance causes the CCW to be created in-process instead of out of process.
Is their a mechanism to force the CCW to be created out-of-process (as if created using CoCreateInstanceEx) with the standard CoCreateInstance call.
I am investigating further, any further you have to add would be greatly appreciated.
Regards
Colin
|
|
|
|
|
Sorry, but my COM knowledge is worse than zero I have no need for it and it looks like too much crap for my liking.
|
|
|
|
|
I would like to play a Direct3D animation inside of a window panel. Can anyone point me in the right direction. I have nothing implemented for that so far. Is it simply a case of specifying that the panel is the Owner of the animation?
|
|
|
|
|
See the Device constructor which takes a Control as the 3rd parameter, which is uses for the rendering surface.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Can anyone give me some information on the commenting schema for the self-generated doc files?
ie> /// <summary> blah </summary>
I need information on what other tags can be used, and what they are for. I can't seem to find any decent documentation on this anywheres online.
Thanks in advance,
Jon
|
|
|
|
|
|
|
You're welcome!
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|