<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/css" href="http://www.pacificingenium.com/Data/style/rss1.css" ?> <?xml-stylesheet type="text/xsl" href="http://www.pacificingenium.com/Data/xsl/rss1.xsl" ?>
<!--RSS generated by mojoPortal Blog Module V 1.0 on Sunday, May 20, 2012-->
<rss version="2.0">
  <channel>
    <title>Technology Blog</title>
    <link>http://www.pacificingenium.com/blog.aspx</link>
    <description />
    <copyright />
    <ttl>120</ttl>
    <managingEditor />
    <generator>mojoPortal Blog Module V 1.0</generator>
    <item>
      <title>TFS Database script error SQL01260: A fatal parser error occurred:.</title>
      <link>http://www.pacificingenium.com/tfs-database-script-error-sql01260-a-fatal-parser-error-occurred.aspx</link>
      <pubDate>Mon, 30 Jan 2012 20:40:51 GMT</pubDate>
      <guid>http://www.pacificingenium.com/tfs-database-script-error-sql01260-a-fatal-parser-error-occurred.aspx</guid>
      <comments>http://www.pacificingenium.com/tfs-database-script-error-sql01260-a-fatal-parser-error-occurred.aspx</comments>
      <description><![CDATA[<p> This blog is specifically written to give pointer and guidance to the dreaded problem SQL01260: A fatal parser error occurred.</p> 

<p>If you have been working with TFS database project, you may have generated data by using the data compare tool. Then in theory, if you put the script generated by data compare feature into Post-Deployment script. Usually this would work, but once awhile, when you include a data script, you get the following error: SQL01260:A fatal error occurred.
</p>

<p>There are a few reason this would happen, for example you have violated db integrity with incorrect forgein keys. But these problems are easy to figure out and fix. However, if you get this error no matter what you do, and you could run the data script in SSMS without failture, then you encountered a TFS bug.</p>

<p>First let me describe the data script. I generated a data script with this in one of the columns -- :$(''''document'''').ready(function() -- I get this script becuase my database keeps some javascript. Now, I can run the script in SSMS, but if I include this script in Script.PostDeployment.sql, TFS database project would not even build.</p>

<p>To solve this problem, I logged a ticket with Microsot support, and looked around internet. There was no way to resolve this. So a Microsoft engineer and I came up with an workaround: replace all $( with "$"(, this will allow you to compile TFS database project and deploy it. After you deploy, you need to do a search and replace to put all "$"( back to $(.</p>

<p>TFS SQL parser is not the same as SQL parser, so it thinks $( as a varaiable replacement, that's why it can't parse this $(document) thing. Of course, you may not have $(, but have other unrecognizable format. One way to figure this out is to run SQLCMD mode, then you can see it fail on the exact line, then apply the fix I moved above.</p>

<p>Finally, if your goal is to use TFS to deploy data, and don't need to have a local deployment working, you could wire up a build process with this data folder, then do a sqlcmd -x deployment. sqlcmd -x will ignore all variable replacement, and hence ignore $(. </p><br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/tfs-database-script-error-sql01260-a-fatal-parser-error-occurred.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>the module was loaded but the call to dllregisterserver failed with error code 0x80070005</title>
      <link>http://www.pacificingenium.com/the-module-was-loaded-but-the-call-to-dllregisterserver-failed-with-error-code-0x80070005.aspx</link>
      <pubDate>Sat, 16 Jul 2011 18:13:03 GMT</pubDate>
      <guid>http://www.pacificingenium.com/the-module-was-loaded-but-the-call-to-dllregisterserver-failed-with-error-code-0x80070005.aspx</guid>
      <comments>http://www.pacificingenium.com/the-module-was-loaded-but-the-call-to-dllregisterserver-failed-with-error-code-0x80070005.aspx</comments>
      <description><![CDATA[<p>When you try to register a dll through regsrv32, and run into this "the module was loaded but the call to dllregisterserver failed with error code" error, mostly likely you are running on Vista or Windows 7. If this is the case, you have have to go to Start->All Programs->Accessories, then right click on the "Command Prompt", choose the "Run as administrator" option.
</p>
<br>
<p>
Now that you have done so, you will see the command promopt showing up with a title "Administrator: Command Prompt", and it will automatically land you in C:\Windows\System32. Then register the dll, the problem should be resolved.
</p><br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/the-module-was-loaded-but-the-call-to-dllregisterserver-failed-with-error-code-0x80070005.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Configuration system failed to initialize</title>
      <link>http://www.pacificingenium.com/configuration-system-failed-to-initialize.aspx</link>
      <pubDate>Sat, 18 Jun 2011 21:38:24 GMT</pubDate>
      <guid>http://www.pacificingenium.com/configuration-system-failed-to-initialize.aspx</guid>
      <comments>http://www.pacificingenium.com/configuration-system-failed-to-initialize.aspx</comments>
      <description><![CDATA[Recently, I ran into this error when I changed the app.conf of a windows console app. 

The error happened when configurationmanager was called in the code like below:
ConfigurationManager.AppSettings["BatchJobInterval"]

This problem was caused by an invalid app.conf. After I straighten out the app.conf, the error disappeared.<br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/configuration-system-failed-to-initialize.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Team Foundation Service Unavailable</title>
      <link>http://www.pacificingenium.com/team-foundation-service-unavailable.aspx</link>
      <pubDate>Mon, 14 Feb 2011 16:36:20 GMT</pubDate>
      <guid>http://www.pacificingenium.com/team-foundation-service-unavailable.aspx</guid>
      <comments>http://www.pacificingenium.com/team-foundation-service-unavailable.aspx</comments>
      <description><![CDATA[<p>I received this "Service Unavailable" error when trying to connect Team Foundation Server 2010.</p>

<P>As a TFS administrator, my first thought is that the system is offline. So I logged on to the TFS server and found that the system is still running, but when I go to IIS and try to run the Web, it doesn't work either.</P>

<P>After much digging, I found out that the application pool for TFS stopped working because I changed the password for the TFSSERVICE account through Server Manager, but didn't change it in the application pool.</P>

<P>Here's how to fix it:</P>
<P>Please note, I'm using IIS7.</P>

<p>1. Open IIS (type InetMgr).</p>
<p>2. Left click on the tfs site that stopped working (for me, the tfs site is called TFS1Server)</p>

<p>3. Select the "Adavnced Settings..." on the "Actions" panel. From this Advanced Settings panel, you can see which Application Pool your TFS website is using. For me, I used the default application pool called "Microsoft Team Foundation Server Application Pool"</p>

<p>4. Now select "Application Pools" in the "Connections" panel. You will see the "Microsoft Team Foundation Server Application Pool" showing up in the Application Pools panel.</p>

<p>5. Select "Microsoft Team Foundation Server Application Pool" and select "Advanced Settings..."</p>

<p>6. In the "Advanced Settings" popup window, locate the "Identity" line, then click on the "..."button.</p>

<p>7. When it pops up, choose Custom Account, select "Set..."</p>

<p>8. In the "Set Credentials" popup screen, set user name and password, such as "TFS1\TFSSERVICE", etc.</p>

<p>9. If you also configured the Web Access Application Pool, then you should set the password for this pool as well. </p>

<p>Restart IIS, you are done.</p>
<br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/team-foundation-service-unavailable.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Project can install dynamically on 2.0 or 3.5</title>
      <link>http://www.pacificingenium.com/project-can-install-dynamically-on-20-or-35.aspx</link>
      <pubDate>Sun, 14 Nov 2010 22:10:26 GMT</pubDate>
      <guid>http://www.pacificingenium.com/project-can-install-dynamically-on-20-or-35.aspx</guid>
      <comments>http://www.pacificingenium.com/project-can-install-dynamically-on-20-or-35.aspx</comments>
      <description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Right click the Setup Project, Select View --&gt; Launch Conditions. </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In the Launch Conditions, right click the .NET Framework, select Properties Window. </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="mso-fareast-language: ZH-CN; mso-no-proof: yes"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id=Picture_x0020_1 style="VISIBILITY: visible; WIDTH: 468pt; HEIGHT: 240pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1026"><v:imagedata o:title="" src="file:///C:\DOCUME~1\UYXH056\LOCALS~1\Temp\msohtmlclip1\01\clip_image001.png"><FONT face=Calibri></FONT></v:imagedata></v:shape></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In the Properties Window, you can select the Framework version as you want.</FONT><BR><BR><A href="http://photo.blog.sina.com.cn/showpic.html#blogid=69838c560100k466&amp;url=http://static4.photo.sina.com.cn/orignal/69838c56g8cc2bbf58f23" target=_blank><SPAN style="COLOR: windowtext; TEXT-DECORATION: none; mso-fareast-language: ZH-CN; mso-no-proof: yes; text-underline: none"><v:shape id=Picture_x0020_5 style="VISIBILITY: visible; WIDTH: 324pt; HEIGHT: 201pt; mso-wrap-style: square" type="#_x0000_t75" o:spid="_x0000_i1025" o:button="t" target="_blank" href="http://photo.blog.sina.com.cn/showpic.html#blogid=69838c560100k466&amp;url=http://static4.photo.sina.com.cn/orignal/69838c56g8cc2bbf58f23" alt="Technique&nbsp;<wbr>blog&nbsp;<wbr>on&nbsp;<wbr>.Net&nbsp;<wbr>VB6&nbsp;<wbr>Interop"><v:fill o:detectmouseclick="t"></v:fill><v:imagedata o:title="Technique&nbsp;<wbr>blog&nbsp;<wbr>on&nbsp;<wbr>" src="file:///C:\DOCUME~1\UYXH056\LOCALS~1\Temp\msohtmlclip1\01\clip_image003.jpg"></v:imagedata></v:shape></SPAN></A><BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"></P><br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/project-can-install-dynamically-on-20-or-35.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Winform dynamically load WPF</title>
      <link>http://www.pacificingenium.com/winform-dynamically-load-wpf.aspx</link>
      <pubDate>Sun, 14 Nov 2010 22:03:40 GMT</pubDate>
      <guid>http://www.pacificingenium.com/winform-dynamically-load-wpf.aspx</guid>
      <comments>http://www.pacificingenium.com/winform-dynamically-load-wpf.aspx</comments>
      <description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In the project, we have a WinForm program and a WPF program. We need to call the WPF in the WinForm code.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The question is the WinForm need to run in .Net Framework2.0 because some of the customer machines only supported Framework2.0.<BR>But as you know, the WPF is only&nbsp;supported in .Net Framework3.5. So I can't directly call the WPF code in the WinForm.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The best way&nbsp;to do that is to dynamically load the WPF dll.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The process is:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Build the WPF program to a WPF.dll </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Dynamically Load the WPF.dll in WinFrom code </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>For example, if the button1 in Winform is clicked, we add the dllinvoke method in the button event method.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>WinForm.cs</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void button1_Click(object sender, EventArgs e)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object[] objParam = new object[1];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objParam[0] = ordInfo;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DllInvoke("WPF.dll", "WPF", "WPFProgram", "showWPF", objParam);</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static object DllInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Assembly DllAssembly = Assembly.LoadFrom(DllFileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type[] DllTypes = DllAssembly.GetTypes();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (Type DllType in DllTypes)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (DllType.Namespace == NameSpace &amp;&amp; DllType.Name == ClassName)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MethodInfo MyMethod = DllType.GetMethod(MethodName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (MyMethod != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (mObject == null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mObject = Activator.CreateInstance(DllType);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return MyMethod.Invoke(mObject, ObjArrayParams);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception mEx)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (object)0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>If you write the code like this, when you run the application and click the button1, you will get an error:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>"System.InvalidOperationException: The calling thread must be STA, because many UI components require this"</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P><SPAN style="FONT-SIZE: 11pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">So we must use a Thread in the&nbsp;button1_Click method to invoke the WPF.dll to solve the problem.<BR>&nbsp;&nbsp;&nbsp;Thread workerThread = new Thread(DisplayItem.ShowWPF);<BR>&nbsp;&nbsp;&nbsp;workerThread.SetApartmentState(ApartmentState.STA);<BR>&nbsp;&nbsp;&nbsp;workerThread.IsBackground = true;<BR>&nbsp;&nbsp;&nbsp;workerThread.Start();<BR style="mso-special-character: line-break"><BR style="mso-special-character: line-break"></SPAN><br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/winform-dynamically-load-wpf.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Interop call C# WinForm by TCP Server</title>
      <link>http://www.pacificingenium.com/interop-call-c-winform-by-tcp-server.aspx</link>
      <pubDate>Sun, 14 Nov 2010 21:58:27 GMT</pubDate>
      <guid>http://www.pacificingenium.com/interop-call-c-winform-by-tcp-server.aspx</guid>
      <comments>http://www.pacificingenium.com/interop-call-c-winform-by-tcp-server.aspx</comments>
      <description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>As my privious section mentioned, if the VB6 and&nbsp;WinForm application&nbsp;are not located in one machine, the WinMsg can't work.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>So there are some remote ways to use:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>TCP communication </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Tcp communication is based on socket communication, but it takes care of all the handshaking and data process for the user. It takes in a stream as input and output a stream as well.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>.Net remoting </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>.Net remoting is similar to web services but it does not have the over head of web service.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Both TCP and .Net remoting are used to pass information between computers accorss network.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Finally we choose to use TCP to handle the work.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The process is:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In the Interop.cs we add a new method&nbsp;named notifyTCPServer, in the method we create a TCPSender</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>and&nbsp;put the message to the TCPSender.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In the TCPSender.cs,&nbsp;it will create a socket to communicate with the remote WinForm machine.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In the remote WinForm machine, we create a TCPReceiver.cs to listen the connection and if the connection is created, it can receive message from the TCPServer. After receive the message, it</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>will send WinMsg to WinForm similar with privious section</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The Interop.cs</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void notifyTCPServer()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringBuilder sbMsg = new StringBuilder();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (this.GenInfo != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; GenInfo.Length; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i == (GenInfo.Length - 1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbMsg.Append(GenInfo[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbMsg.Append(GenInfo[i] + "|");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger.WriteEntry("GenInfo:" + sbMsg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbMsg.Append(Flag.ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //send msg through TCP server</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCPSender tcpSender = new TCPSender();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tcpSender.sendMsg(sbMsg.ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The TCPSener.cs<BR>&nbsp;&nbsp;&nbsp; public class TCPSender<BR>&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int port = 9050;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void sendMsg(String msg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //the Remote machine IP</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string ip= "192.168.1.100";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendToClient(ip, msg);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void sendToClient(string ip, string msg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TcpClient client = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetworkStream strm = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client = new TcpClient();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IPAddress address = IPAddress.Parse(ip);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IAsyncResult MyResult = client.BeginConnect(ip, port, null, null);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyResult.AsyncWaitHandle.WaitOne(5000, true);&nbsp;&nbsp; //set time out&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!MyResult.IsCompleted)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the formatter that will serialize my object on my stream&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IFormatter formatter = new BinaryFormatter();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strm = client.GetStream(); // the stream&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; formatter.Serialize(strm, msg); // the serialization process&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception e)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (strm != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strm.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (client != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The Remothe TCPReceiver.cs</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp; public class TCPReceiver<BR>&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static string IP = "";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int listenPort = 9050;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void listen()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TcpListener listener = new TcpListener(listenPort);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listener.Start();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TcpClient client = listener.AcceptTcpClient();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IPEndPoint ip = (IPEndPoint)client.Client.RemoteEndPoint;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IP = ip.Address.ToString();//get the IP from client<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetworkStream strm = client.GetStream();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IFormatter formatter = new BinaryFormatter();</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String msg = (String)formatter.Deserialize(strm);</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Interop interop = new Interop ();</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; interop.notifyCSharpFormByTCP(msg);</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strm.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp; }</FONT></P><br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/interop-call-c-winform-by-tcp-server.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Interop call C# WinForm by WinMsg</title>
      <link>http://www.pacificingenium.com/interop-call-c-winform-by-winmsg.aspx</link>
      <pubDate>Wed, 15 Sep 2010 04:29:57 GMT</pubDate>
      <guid>http://www.pacificingenium.com/interop-call-c-winform-by-winmsg.aspx</guid>
      <comments>http://www.pacificingenium.com/interop-call-c-winform-by-winmsg.aspx</comments>
      <description><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>2. Interop call C# WinForm by WinMsg</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Now we have a COM interop in VB6, VB6 can put the GenInfo and Flag to the C# class. But how can</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>I notify our WinForm application to get these values?</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>I decide&nbsp;to to use&nbsp;WinMsg to communicate between the Interop.cs and WinForm.cs, because they are all written in C#. It's an easily way to implement if the VB6 and WinForm located in one machine. But if they are not located in one machine, we must use another way I will mention in next section.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The process is:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Firstly, in the sender side, we use FindWindow to find the WinForm Handler, then send the WM_COPYDATA message to the&nbsp;receiver use the method SendMessage.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>The methods FindWindow and SendMessage are Windows&nbsp;API in User32.dll.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><BR><FONT face=Calibri>Then the receiver side(WinFrom) will get the message in the method DefWndProc.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>We can use the overriding method&nbsp;DefWndProc of WinForm&nbsp;to handle customer defined message.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>In both sender and receiver we use the same struct COPYDATASTRUCT to deliver the message.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Sender: the Interop.cs</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const int WM_COPYDATA = 0x004A;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private string CSHARP_WINDOWTITLE_SERVER = "C#-WinForm";</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void notifyCSharpForm()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder sbMsg = new StringBuilder();</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (this.GenInfo != null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; GenInfo.Length; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i == (GenInfo.Length - 1))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbMsg.Append(GenInfo[i]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbMsg.Append(GenInfo[i] + "|");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sbMsg.Append(Flag.ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Notify the WinForm to get the GenInfo and Flag</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int WINDOW_HANDLER = FindWindow(null, CSHARP_WINDOWTITLE_SERVER);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (WINDOW_HANDLER != 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; COPYDATASTRUCT cds = new COPYDATASTRUCT();</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cds.dwData = (IntPtr)100;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cds.lpData = sbMsg.ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cds.cbData = sbMsg.Length + 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public struct COPYDATASTRUCT<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IntPtr dwData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int cbData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [MarshalAs(UnmanagedType.LPStr)]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string lpData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [DllImport("User32.dll", EntryPoint = "SendMessage")]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private static extern int SendMessage(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int hWnd, // handle to destination window<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int Msg, // message<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int wParam, // first message parameter<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ref COPYDATASTRUCT lParam // second message parameter<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [DllImport("User32.dll", EntryPoint = "FindWindow")]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private static extern int FindWindow(string lpClassName, string<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lpWindowName);</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Receiver: WinForm.cs</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private string CSHARP_WINDOWTITLE_SERVER = "C#-WinForm";</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public frmMain()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ......</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Text = CSHARP_WINDOWTITLE_SERVER;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected override void DefWndProc(ref System.Windows.Forms.Message m)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch (m.Msg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case WM_COPYDATA:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; COPYDATASTRUCT mystr = new COPYDATASTRUCT();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type mytype = mystr.GetType();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mystr = (COPYDATASTRUCT)m.GetLParam(mytype);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String msg = mystr.lpData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[] msgArr = msg.Split('|');<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenInfo = msgArr;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Do something to display the GenInfo<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base.DefWndProc(ref m);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><BR><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public struct COPYDATASTRUCT<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public IntPtr dwData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int cbData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [MarshalAs(UnmanagedType.LPStr)]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string lpData;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P><br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/interop-call-c-winform-by-winmsg.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>C# VB6 interop</title>
      <link>http://www.pacificingenium.com/c-vb6-interop.aspx</link>
      <pubDate>Mon, 30 Aug 2010 20:33:15 GMT</pubDate>
      <guid>http://www.pacificingenium.com/c-vb6-interop.aspx</guid>
      <comments>http://www.pacificingenium.com/c-vb6-interop.aspx</comments>
      <description><![CDATA[<P class=MsoListParagraph style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri>VB6 call C# by interop</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Continue from the last blog. The five things we need to achieve are:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Due to the technical requirement and the business needs, we have decided to implement the following:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri>Created an interop to facilitate communicate between VB6 and Bumble Bee</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri>Use WinMsg to communicate between interop and Winform for .net2.0 – this is to ensure that if Bumble Bee exists on the same machien as the VB6 program, the two programs will be the minimum lag.</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri>3.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri>Interop call C# WinForm by TCP Server – if the Bumble Bee program does not co-locate on the VB6 program’s machine, we need to use TCP Server/Listener technology to do it.</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri>4.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri>Winform dynamically load WPF – most of the funcationlity will be implemented on the Winform which runs on the required .net2.0. For the 3.5 funcationlity that requires WPF, we will simply call WPF through Winform when we can dynamically determine if .net 3.5 exists or not on the particular machine.</FONT></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri>5.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri>To ensure step 4, we have to make sure tha the project can install dynamically on 2.0 or 3.5.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Today, let’s talk about 1:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>As you know, we can expose the .NET Framework components to COM. Then the VB6 code could consume it as usual.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Perform the following steps:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Create a .Net class libray project just like normally. </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Open the&nbsp;properties of the project and click the Build tab. Make sure the Register For COM Interop option selected.<BR></FONT><A href="http://photo.blog.sina.com.cn/showpic.html#blogid=69838c560100k466&amp;url=http://static12.photo.sina.com.cn/orignal/69838c56g8b6e62fc62fb" target=_blank><SPAN style="COLOR: windowtext; TEXT-DECORATION: none; mso-fareast-language: ZH-CN; mso-no-proof: yes; text-underline: none"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id=Picture_x0020_3 style="VISIBILITY: visible; WIDTH: 512.25pt; HEIGHT: 212.25pt; mso-wrap-style: square" o:button="t" target="_blank" href="http://photo.blog.sina.com.cn/showpic.html#blogid=69838c560100k466&amp;url=http://static12.photo.sina.com.cn/orignal/69838c56g8b6e62fc62fb" alt="Technique&nbsp;<wbr>blog&nbsp;<wbr>on&nbsp;<wbr>.Net&nbsp;<wbr>VB6&nbsp;<wbr>Interop" type="#_x0000_t75" o:spid="_x0000_i1025"><FONT face=Calibri><v:fill o:detectmouseclick="t"></v:fill><v:imagedata src="file:///C:\DOCUME~1\UYXH056\LOCALS~1\Temp\msohtmlclip1\01\clip_image001.jpg" o:title="Technique&nbsp;<wbr>blog&nbsp;<wbr>on&nbsp;<wbr>"></v:imagedata></FONT></v:shape></SPAN></A></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Create a .Net Class</FONT><SPAN style="FONT-FAMILY: 'MS Mincho'; mso-bidi-font-family: 'MS Mincho'">：</SPAN><FONT face=Calibri> </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>using System;<BR>using System.Collections.Generic;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>using System.Text;<BR>using System.Runtime.InteropServices;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>namespace InteropLib<BR>{<BR>&nbsp;&nbsp;&nbsp; public interface IInterop<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string[] GenInfo { get; set; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int Flag { get; set; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void notifyCSharpForm();<BR>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp; [GuidAttribute("D5E4C17E-3C1C-4519-AC39-F7749CE81AAB")]<BR>&nbsp;&nbsp;&nbsp; public class Interop : IInterop<BR>&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private string[] _GenInfo = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int _flag = 0;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string[] GenInfo<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { return _GenInfo;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {_GenInfo = value;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int Flag<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {return _flag;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {_flag = value;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void notifyCSharpForm()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Notify the WinForm to get the GenInfo and Flag</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .....</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><BR><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>}</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>If you compile the application with the code just shown and there are no errors, the assembly will be visible to COM.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Consume it in VB6 </FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Dim ocdInterop As InteropLib.Interop</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Private Sub cmdFinish_Click()</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;Set interop = New InteropLib.Interop&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;interop .Flag = -1</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri></FONT>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Some Tips:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Tip 1. After compiling your assembly to a DLL, you can register it for COM Interop from the command line:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; regasm c:\path\MyDll.dll /tlb:MyDll.tlb</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>Tip 2.&nbsp;Add GuidAtrribute for the assembly, then it will keep a unique reference for VB6.<BR>If the application is recompiled, no need to recomiple the&nbsp;VB6 code&nbsp;again. Because the GUIDs is always same for VB6.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [GuidAttribute("D5E4C17E-3C1C-4519-AC39-F7749CE81AAB")]&nbsp;</FONT></P>
<P>&nbsp;</P>
<br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/c-vb6-interop.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>VB6 C# interop, winmsg/TCP server/listner, .net2.0/3.5 mix </title>
      <link>http://www.pacificingenium.com/vb6-c-interop-winmsgtcp-serverlistner-net2035-mix-.aspx</link>
      <pubDate>Sun, 15 Aug 2010 21:15:47 GMT</pubDate>
      <guid>http://www.pacificingenium.com/vb6-c-interop-winmsgtcp-serverlistner-net2035-mix-.aspx</guid>
      <comments>http://www.pacificingenium.com/vb6-c-interop-winmsgtcp-serverlistner-net2035-mix-.aspx</comments>
      <description><![CDATA[<p>Integrate vb6, interop, winmsg, tcp, .net2.0/3.5 together into a real life project</p>

<p>In a recent project, we have encountered a very complex business needs that presented a number of obstacles to our developers.  Through much trial and error through, we overcame the obstacles one by one, and finally triumphed the “evil” and in the process learnt a lot of nuances of vb6 + .net.</p>

<p>Before I start to go into the technical details, I’ll first talk briefly the business needs the project tried to answer. Although due to confidentiality, I have to greatly simply the details, I hope my explaination will capture the big picture.</p>
<p>A client has an old VB6 code that does roughly 3 things:</p>
<p> &nbsp;&nbsp;&nbsp;&nbsp;1.	Take a series of user input through a complex UI</p>
<p> &nbsp;&nbsp;&nbsp;&nbsp 2.	Call database, do some business logic processing</p>
<p> &nbsp;&nbsp;&nbsp;&nbsp 3.	Output the result in both text and video (windows media and flash) format to a number of screens, which may or may not be attached to the computer the VB6 program is running on.</p>

<p>Our job is to create a program - let’s called “Bumble Bee” because how busy it is - will run on all the screens connected to the VB6 code. “Bumble Bee” will read in the input the VB6 provides and display text and play the right videos. Sounds simple enough? So we thought.
There are some requirements about this program.
<p>A.	It has to be light weight, very light. Because the screens that run this program has very little processing power.</p>
<p>B.	It has to be very responsive. Think of first person shooter type of responsiveness. As soon as a user enters something on the VB6, “Bumble Bee” needs to act and display. If a user continuously hits the UI input on the VB6 code,  Bumble Bee needs to get real busy and flash through tons of messages and decide what video to play.
<p>C.	Bumble Bee exists in both .net 2.0 and .net 3.5 world due to legacy reasons. On .net 2.0, the client wants a winform to display text and videos. On .net3.5, they want a WPF so that it can deal with screen rotation, but as we know, WPF cannot be run on .net2.0. So suddenly, Bumble Bee needs to have two different versions, is this acceptable?
</p>
<p>Due to the technical requirement and the business needs, we have decided to implement the following:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp 1.	Created an interop to facilitate communicate between VB6 and Bumble Bee</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp 2.	Use WinMsg to communicate between interop and Winform for .net2.0 – this is to ensure that if Bumble Bee exists on the same machien as the VB6 program, the two programs will be the minimum lag.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp 3.	Interop call C# WinForm by TCP Server – if the Bumble Bee program does not co-locate on the VB6 program’s machine, we need to use TCP Server/Listener technology to do it.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp 4.	Winform dynamically load WPF – most of the funcationlity will be implemented on the Winform which runs on the required .net2.0. For the 3.5 funcationlity that requires WPF, we will simply call WPF through Winform when we can dynamically determine if .net 3.5 exists or not on the particular machine.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp 5.	To ensure step 4, we have to make sure tha the project can install dynamically on 2.0 or 3.5.</p>

<p>This is the summary of what what we decided to do. In the following days, I’ll continue the blog and fill the details of the trial and error we had to walk through to get the project completed. Stay tuned… </p>
<br /><a href='http://www.pacificingenium.com'>Henry</a>&nbsp;&nbsp;<a href='http://www.pacificingenium.com/vb6-c-interop-winmsgtcp-serverlistner-net2035-mix-.aspx'>...</a>]]></description>
    </item>
  </channel>
</rss>
