{"id":40,"date":"2010-05-07T14:39:11","date_gmt":"2010-05-07T19:39:11","guid":{"rendered":"http:\/\/capnbry.net\/blog\/?p=40"},"modified":"2010-05-07T14:39:11","modified_gmt":"2010-05-07T19:39:11","slug":"determining-threadingmodel-in-a-delphi-application","status":"publish","type":"post","link":"http:\/\/capnbry.net\/blog\/?p=40","title":{"rendered":"Determining ThreadingModel in a Delphi Application"},"content":{"rendered":"<p>I has occurred to me that someone might need to determine their COM threading model from inside their application.  This could be useful for some ASSERT() code or perhaps as part of a unit test.  From <a href=\"http:\/\/capnbry.net\/blog\/?p=18\">my last blog post<\/a>, you&#8217;ll recall that this information is stored in an opaque structure located in each thread&#8217;s Thread Environment Block (TEB).  Actually quite simple to do, with original credit going to <a href=\"http:\/\/www.microsoft.com\/msj\/1099\/bugslayer\/bugslayer1099.aspx\">John Robbins&#8217; Bugslayer Column<\/a> from the old MSJ magazine.<\/p>\n<pre class=\"brush:delphi\">\r\n{\r\nuses ActiveX;\r\nPorted from John Robbins - Microsoft Systems Journal Bugslayer Column - October '99\r\nhttp:\/\/www.microsoft.com\/msj\/1099\/bugslayer\/bugslayer1099.aspx\r\n}\r\nfunction DebugCoGetThreadingModel : integer;\r\nconst\r\n  OLE_APT_MASK  = $0080;\r\n  OLE_FREE_MASK = $0140;\r\nvar\r\n  dwOLETLS: Cardinal;\r\n  dwFlags:  Cardinal;\r\nbegin\r\n\r\n  asm\r\n    \/\/ Get TEB\r\n    mov eax, FS:[018h]\r\n    mov eax, [eax+0f80h]\r\n    mov dwOLETLS, eax\r\n  end;\r\n\r\n  { Not initialized }\r\n  if dwOLETLS = 0 then begin\r\n    Result := -1;\r\n    exit;\r\n  end;\r\n\r\n  dwFlags := PCardinal((dwOLETLS + $0C))^;\r\n\r\n  if ((dwFlags and OLE_APT_MASK) = OLE_APT_MASK) then\r\n    Result := COINIT_APARTMENTTHREADED\r\n\r\n  else if ((dwFlags and OLE_FREE_MASK) = OLE_FREE_MASK) then\r\n    Result := COINIT_MULTITHREADED\r\n\r\n  { Unknown }\r\n  else\r\n    Result := -2;\r\nend;\r\n<\/pre>\n<p>And some sample usage code<\/p>\n<pre class=\"brush:delphi\">\r\n  case DebugCoGetThreadingModel of\r\n    -2: Label1.Caption := 'Unknown';\r\n    -1: Label1.Caption := 'Not initialized';\r\n    COINIT_APARTMENTTHREADED: Label1.Caption := 'COINIT_APARTMENTTHREADED';\r\n    COINIT_MULTITHREADED: Label1.Caption := 'COINIT_MULTITHREADED';\r\n  end;\r\n<\/pre>\n<p>John also has an <a href=\"http:\/\/www.wintellect.com\/cs\/blogs\/jrobbins\/default.aspx\">excellent blog of his own<\/a> which is which is a fantastic low-level technical resource.  Speaking of old magazines, I just discovered the <em>Bug of the Month<\/em> ad that has appeared in Dr Dobbs for nearly the past 20 years has <a href=\"http:\/\/www.gimpel.com\/html\/bugs.htm\">an online archive<\/a>.  Head over to Gimpel Software if you&#8217;re up for some C-based brain exercises.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I has occurred to me that someone might need to determine their COM threading model from inside their application. This could be useful for some ASSERT() code or perhaps as part of a unit test. From my last blog post, you&#8217;ll recall that this information is stored in an opaque structure located in each thread&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/40"}],"collection":[{"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":3,"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":43,"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions\/43"}],"wp:attachment":[{"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/capnbry.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}