C#中通过Process启动外部第三方程序MainWindowHandle句柄为0

2014年05月11日 其他资源 15条评论 阅读16117次
C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0
前几天遇到了一个在C#的winform程序中,启动一个第三方jar程序,并修改jar运行窗体的标题的问题。
大体的需求就是上面说明的那样,下面是当初设想的思路和实现代码。
STEP1
// 根据jar文件的路径,启动该jar
Process p = Process.Start(jarfilePath);

STEP2
// 引用WindowsAPI中SetWindowText的声明
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowText")]
public static extern int SetWindowText(IntPtr hwnd, String lpString);

STEP3
// 得到启动的jar窗体的句柄
IntPtr hwnd = p.MainWindowHandle;

STEP4
// 改变jar窗体的标题栏文字
SetWindowText(hwnd, "标题栏新文字");

通过上面这四个步骤,感觉应该就可以改变启动的外部第三方程序的窗口标题栏了。
但,实际运行结果却不是想象的那样。追究为什么启动的外部第三方应用程序窗体的标题栏文字为什么没有改变,是哪行代码没被执行到,或是哪行代码出错了,在DEBUG单步执行时,却又没发现任何问题,新窗体的句柄也正常,执行完上面第四步骤的那行代码之后,看新窗体的标题栏文字,完美地被改变了。
百思不得其解,接下来的思路就是看不DEBUG单步执行时,哪行出了问题,于是在关键步骤3的后面,添加了下面的调试输出代码
Console.WriteLine("handle>>>" + hwnd);
这时的执行结果果然暴漏了问题所在,调试语句输出的MainWindowHandle为0.

接下来问题的关键就是找到为什么Process.MainWindowHandle得到的窗体句柄是0的问题的原因
几经google之后,发现了问题的原因,国外专家给出的原因解释,感觉还挺靠谱的。

The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.

大体意思就是通过Process.Start得到的process还没在任务栏上创建出icon来,所以这个process貌似就被认为是不可见的,所以得到的句柄就是0.
看到这里就很容易理解为什么在DEBUG单步调试时,Process.MainWindowHandle是正常的原因了
基于上面这些解释,找到了在STEP3之前增加一下代码的解决方案。
while (p.MainWindowHandle == IntPtr.Zero)
                {
                    System.Threading.Thread.Sleep(100);
                }

到此,问题解决。但仍残留了一个值得思考的问题。在Process类下有WaitForInputIdle()这个方法,看msdn上的解释貌似也是等待,我尝试着不用上面那个while循环,而用p.WaitForInputIdle();来代替,结果是不行的。
为什么呢,希望有对此熟悉的朋友能给个解释。
分享本文至:

WRITTEN BY

avatar
本文标签:C#winformHandle
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

15 条评论

  1. <a href="http://www.lateraline.com/nike-free-run-5.0-neon-green-and-blue-trainersa">nike free run 5.0 neon green and blue</a> <a href="http://www.naijareward.com/14-elite-vincent-brown-indianapolis-colts-youth-jersey-home-royal-blue-nfld">14 elite vincent brown indianapolis colts youth jersey home royal blue</a> <a href="http://www.smsteachers.com/nike-air-max-87-sb-sliver-grau-gr%C3%BCn-chaussuresf">nike air max 87 sb sliver grau gr眉n</a> <a href="http://www.alfiekaye.com/air-jordan-after-game-i-yellow-green-trainerse">air jordan after game i yellow green</a> <a href="http://www.eatisrael.com/tory-burch-striped-bag-toryburchr">tory burch striped bag</a> <a href="http://www.introtecint.com/yeezy-boost-750-rosso-october-build-butye">yeezy boost 750 rosso october build</a>
    cafedegino http://www.cafedegino.com/

  2. <a href="http://www.goseecreate.com/nike-air-max-bw-ultra-se-herren-schuh-polish-kurpesb">nike air max bw ultra se herren schuh polish</a> <a href="http://www.klarinettino.com/nike-cortez-gold-zone-scarpef">nike cortez gold zone</a> <a href="http://www.loydartist.com/hermes-evelyne-29-price-journal-hermesa">hermes evelyne 29 price journal</a> <a href="http://www.musicbreeds.com/nike-air-presto-anti-fur-lyser%C3%B8d-brun-chaussuresa">nike air presto anti fur lyser酶d brun</a> <a href="http://www.plakglas.com/nike-roshe-woman-tumblr-tumblr-couple-shoese">nike roshe woman tumblr tumblr couple</a> <a href="http://www.samuelzide.com/ugg-classic-short-metallic-boots-india-uggsb">ugg classic short metallic boots india</a>
    beachtimenow http://www.beachtimenow.com/

  3. <a href="http://www.isnowbot.com/hermes-lindy-30-hermesr">hermes lindy 30</a> <a href="http://www.rawnessoflife.com/nike-air-max-2015-orange-and-jaune-urine-chaussuresd">nike air max 2015 orange and jaune urine</a> <a href="http://www.padcrashin.com/nike-lebron-13-pot-of-gold-yeller-sportsf">nike lebron 13 pot of gold yeller</a> <a href="http://www.maxcologne.com/ugg-bailey-3-button-sale-date-uggsd">ugg bailey 3 button sale date</a> <a href="http://www.reinerbrach.com/adidas-kinder-schuhe-gr%C3%BCn-schwarz-butyg">adidas kinder schuhe gr眉n schwarz</a> <a href="http://www.naturalcitral.com/hombresta-verde-jordan-10-butya">hombresta verde jordan 10</a>
    borumdeluxe http://www.borumdeluxe.com/

  4. <a href="http://www.plunderpuss.com/air-jordan-8-wolf-gr%C3%A5-kvinners-footc">air jordan 8 wolf gr氓 kvinners</a> <a href="http://www.crgrentals.com/nike-air-max-90-kpu-verde-plata-butym">nike air max 90 kpu verde plata</a> <a href="http://www.zszlubanie.com/parajumpers-anchorage-jacket-ladies-parajumpersb">parajumpers anchorage jacket ladies</a> <a href="http://www.vincentclerc.com/canada-goose-chateau-parka-edmonton-phone-number-canadagooseb">canada goose chateau parka edmonton phone number</a> <a href="http://www.revenueaco.com/air-max-90-em-grigio-bianca-kurpese">air max 90 em grigio bianca</a> <a href="http://www.atouchofyoga.com/pandora-charms-herzen-set-liebe-freundschaft-pandoral">pandora charms herzen set liebe freundschaft</a>
    maudehairva http://www.maudehairva.com/

  5. avatar expartyrental

    <a href="http://www.scrgruppen.com/naranja-polo-sweater-poloa">naranja polo sweater</a> <a href="http://www.jklindfors.com/nike-lebron-13-gs-wolf-gris-print-scarpek">nike lebron 13 gs wolf gris print</a> <a href="http://www.lorenaflowers.com/blumen-kleider-nyc-dresse">blumen kleider nyc</a> <a href="http://www.suppersu.com/air-jordan-melo-m11-grau-orange-footi">air jordan melo m11 grau orange</a> <a href="http://www.riverfresno.com/zx-850-gr%C3%BCn-butyg">zx 850 gr眉n</a> <a href="http://www.blecamerica.com/parajumpers-coat-xs-express-parajumpersb">parajumpers coat xs express</a>
    expartyrental http://www.expartyrental.com/

  6. avatar proseccoshow

    <a href="http://www.expartyrental.com/kyrie-3-p%C3%BArpura-footc">kyrie 3 p煤rpura</a> <a href="http://www.kbieurope.com/air-jordan-6-r%C3%B8d-brun-butym">air jordan 6 r酶d brun</a> <a href="http://www.kathybphd.com/parajumpers-kodiak-jacket-parajumpers-jacka-rea-parajumpersb">parajumpers kodiak jacket parajumpers jacka rea</a> <a href="http://www.energyhotshot.com/canada-goose-down-hoody-definition-government-canadagooseb">canada goose down hoody definition government</a> <a href="http://www.sgwoods.com/nike-huarache-negro-us-kurpese">nike huarache negro us</a> <a href="http://www.carraignuabnb.com/pandora-mom-charms-earrings-wholesale-pandoral">pandora mom charms earrings wholesale</a>
    proseccoshow http://www.proseccoshow.com/

  7. avatar tdsurvivalkit

    <a href="http://www.noumoles.com/zx-flux-schwarz-silber-schuheb">zx flux schwarz silber</a> <a href="http://www.alannacaldas.com/maschio-ralph-lauren-bianca-polo-t-shirt-with-crew-neck-polou">maschio ralph lauren bianca polo t shirt with crew neck</a> <a href="http://www.texastribal.com/hermes-scarf-authenticate-key-scarfb">hermes scarf authenticate key</a> <a href="http://www.tinystorygame.com/nike-dam%C3%A4nner-free-4.0-flyknit-laufen-schuh-wei%C3%9F-chaussuresb">nike dam盲nner free 4.0 flyknit laufen schuh wei脽</a> <a href="http://www.lifeofeighty.com/ray-ban-aviator-mirror-rb3025-sunglassesr">ray ban aviator mirror rb3025</a> <a href="http://www.lmbaidu.com/north-face-vest-measurements-for-sale-northfacea">north face vest measurements for sale</a>
    tdsurvivalkit http://www.tdsurvivalkit.com/

  8. avatar allstudentshuttle

    <a href="http://www.mipaneldecontrol.net/asics-gel-kinsei-2-hommes-fonctionnement-chaussures-up-kurpese">asics gel kinsei 2 hommes fonctionnement chaussures up</a> <a href="http://www.moombaki.net/le-coq-sportif-sko-lilla-schuheg">le coq sportif sko lilla</a> <a href="http://www.mtcsolutions.net/air-jordan-1-sort-guld-r%C3%B8d-scarpef">air jordan 1 sort guld r酶d</a> <a href="http://www.muftimuneer.net/nike-air-max-camouflage-for-verkauf-kurpesa">nike air max camouflage for verkauf</a> <a href="http://www.mypotatocrop.net/nike-free-3.0-or-noir-kurpesd">nike free 3.0 or noir</a> <a href="http://www.nicholasbasbanes.net/nike-zoom-structure-19-kvinders-bl%C3%A5-himmelbl%C3%A5-obuva">nike zoom structure 19 kvinders bl氓 himmelbl氓</a>
    allstudentshuttle http://www.allstudentshuttle.net/

  9. avatar fauconneriemarche

    <a href="http://www.maxicontainer.net/herre-supra-skytop-guld-lilla-butya">herre supra skytop guld lilla</a> <a href="http://www.mcrocks.net/air-jordan-18-wei%C3%9F-varsity-rot-for-verkauf-obuvd">air jordan 18 wei脽 varsity rot for verkauf</a> <a href="http://www.medicalgenius.net/air-jordan-retro-9-oro-brosso-scarpec">air jordan retro 9 oro brosso</a> <a href="http://www.meganhaneydesigns.net/nike-mercurial-vapor-viii-negro-sg-schuhef">nike mercurial vapor viii negro sg</a> <a href="http://www.mendiexpo.net/nike-internationalist-glacier-blau-uniform-scarpef">nike internationalist glacier blau uniform</a> <a href="http://www.mfofoundation.net/adidas-prougeator-powerswerve-david-beckham-scarpeg">adidas prougeator powerswerve david beckham</a>
    fauconneriemarche http://www.fauconneriemarche.net/

  10. avatar elazigled

    <a href="http://www.sofakolle.com/toronto-maple-leafs-heritage-jersey-nflh">toronto maple leafs heritage jersey</a> <a href="http://www.tomsavagetrio.com/reekbok-revenge-plus-vintage-blau-lila-shoese">reekbok revenge plus vintage blau lila</a> <a href="http://www.hopeforcinema.com/polo-ralph-lauren-sunglasses-3041-black-shades-by-ralph-lauren-poloh">polo ralph lauren sunglasses 3041 black shades by ralph lauren</a> <a href="http://www.pusulagelisim.com/2014-nike-air-yeezy-ii-2-sp-max-90-the-devil-series-west-all-r%C3%B8d-foota">2014 nike air yeezy ii 2 sp max 90 the devil series west all r酶d</a> <a href="http://www.ndtkalashop.com/jordan-retro-11-gamma-azul-beanie-runninga">jordan retro 11 gamma azul beanie</a> <a href="http://www.isgmembers.com/reebok-hexaffect-run-gr%C3%A5-r%C3%B8d-scarpef">reebok hexaffect run gr氓 r酶d</a>
    elazigled http://www.elazigled.com/

  11. avatar promoldgauer

    <a href="http://www.volcanosmap.com/nike-air-presto-kvinners-svart-obuvk">nike air presto kvinners svart</a> <a href="http://www.machinband.com/air-jordan-7-hare-nero-scarpei">air jordan 7 hare nero</a> <a href="http://www.networkqoffer.com/asics-gel-lyte-negro-zapatosh">asics gel lyte negro</a> <a href="http://www.tlsfirm.com/nike-free-5.0-m%C3%A6nd-gr%C3%A5-fluorescent-gul-skok">nike free 5.0 m忙nd gr氓 fluorescent gul</a> <a href="http://www.foxxology.com/kvinders-nike-free-run-3-oransje-obuvi">kvinders nike free run 3 oransje</a> <a href="http://www.joyportland.com/nike-air-max-90-zapatilla-de-deporteboot-gris-verde-zapatosl">nike air max 90 zapatilla de deporteboot gris verde</a>
    promoldgauer http://www.promoldgauer.com/

  12. avatar meetspotme

    <a href="http://www.delicouncil.com/air-jordan-retro-12-sort-lyser%C3%B8d-skoj">air jordan retro 12 sort lyser酶d</a> <a href="http://www.jeffaudette.com/polo-v-neck-t-shirts-poloh">polo v neck t shirts</a> <a href="http://www.ezappspro.com/air-jordan-retro-11-grey-wool-shoesi">air jordan retro 11 grey wool</a> <a href="http://www.susannekober.com/bianca-and-oro-adidas-gazelle-scarpej">bianca and oro adidas gazelle</a> <a href="http://www.kingsroomlv.com/m%C3%A4nner-nike-kyrie-1-blau-gr%C3%BCn-schuhej">m盲nner nike kyrie 1 blau gr眉n</a> <a href="http://www.sadecemerak.com/clemson-tigers-47-ncaa-47-womens-shine-on-cap-hath">clemson tigers 47 ncaa 47 womens shine on cap</a>
    meetspotme http://www.meetspotme.com/

  13. avatar bigdstattoos

    <a href="http://www.seocontenidos.com/hommes-nike-air-rejuven8-mule-3-le-sableals-rouge-blanc-chaussuresl">hommes nike air rejuven8 mule 3 le sableals rouge blanc</a> <a href="http://www.elitexgaming.com/adidas-superstar-rot-suede-schuhej">adidas superstar rot suede</a> <a href="http://www.chezpenny.com/air-jordan-after-game-wei%C3%9F-schwarz-rot-schuhei">air jordan after game wei脽 schwarz rot</a> <a href="http://www.williherren.com/menn-nike-air-max-2014-oransje-svart-gr%C3%B8nn-obuvj">menn nike air max 2014 oransje svart gr酶nn</a> <a href="http://www.alimonis.com/air-jordan-1-phat-nero-stealth-court-viola-scarpel">air jordan 1 phat nero stealth court viola</a> <a href="http://www.galvomode.com/hommes-kevin-durant-7-bleu-or-noir-chaussuresh">hommes kevin durant 7 bleu or noir</a>
    [url=http://www.bigdstattoos.com/]bigdstattoos[/url]

  14. avatar koningsheim

    <a href="http://www.vbmconseil.com/air-jordan-xi-retro-low-skox">air jordan xi retro low</a> <a href="http://www.davidmyre.com/billig-nike-air-force-1-dam%C3%A4nner-gr%C3%BCn-sportsd">billig nike air force 1 dam盲nner gr眉n</a> <a href="http://www.wsendeavors.com/coach-gold-crossbody-bag-coachr">coach gold crossbody bag</a> <a href="http://www.betroopsports.com/nike-cowboys-21-ezekiel-elliott-white-vapor-untouchable-limited-jersey-nfle">nike cowboys 21 ezekiel elliott white vapor untouchable limited jersey</a> <a href="http://www.rbengals.com/jordan-11-gold-black-ovo-sportsb">jordan 11 gold black ovo</a> <a href="http://www.nickebrown.com/jordan-true-flight-outfit-black-purple-trainersf">jordan true flight outfit black purple</a>
    koningsheim http://www.koningsheim.com/

  15. avatar 金才巫白

    看起来它是好的,其实它是坏的。——菲律宾申博sunbetshenbo.net

欢迎留言




用户登录

sitemap