# Other Languages > C and C++ >  DirectCom.dll implementation

## smkperu

Hello Olaf,

   I have  gone thru your *VB6ThreadingDirectCOM* sample *https://www.vbforums.com/showthread....-dll-HelperLib* 
which is really awesome.

Can you provide *source code* for the exported functions say *STARTCOMOBJECT* of *DirectCom.dll* c/c++ dll  so that I can improve my understanding of  *COM* using *c/c++*.

Thanks

----------


## Schmidt

It's the standard CreateThread-API-Call which is happening there basically...

And in the ThreadCallback-Function the usual things happen.
- Take the passed UDT-Pointer from the "UserParam" (priorily passed into CreateThread)
- Read out: DllName, ClassName, "Users-UserParam" from the UDT-Fields inside this new Thread
- initialize the Thread for COM-STA-Mode
- create an STA-ClassInstance regfree (from DllName, ClassName)
- enter the Thread-MessageLoop and stay there (until notice from the "controlling Class on the outside")

On Startup (from the outside), the UDT is filled from inside a Helper-Class cThreadHandler - 
which will be returned by this Function - and runs on the MainThread (allowing interaction with the started Thread).

And that's it - not Rocket-Science.

Olaf

----------


## smkperu

Dear Olaf,

  Thanks for the clarification.
  I understood your explanation *even better* when I came across your another awesome pure 
VB6 Threading-Tutorial (Mandelbrot-Rendering) at the link
https://www.vbforums.com/showthread....rot-Rendering) at Step7 StdExe (using STA-Dll).

More or less both VB6ThreadingDirectCOM and VB6 Threading-Tutorial (Mandelbrot-Rendering) at the link
https://www.vbforums.com/showthread....rot-Rendering) at Step7 StdExe (using STA-Dll) are same except
that VB6ThreadingDirectCOM is using c/c++ dll (DirectCom.dll) for creating the STA thread.

The *beauty* of VB6ThreadingDirectCOM  is that it *works fine* even when the project is compiled to *p-code*(both std exe app  and STA dll app) and works even when run in vb6 IDE.

But Step7 StdExe (using STA-Dll) of VB6 Threading-Tutorial (Mandelbrot-Rendering) *crashes* when run outside or in vb6 IDE  when compiled to *p-code*.

Is there any reason for this since I don't have this problem when working in pure c/c++ applications in which i never come across *p-code* option while compiling in vc++ IDE like in vb6 IDE.


Thanks.

----------


## Schmidt

> https://www.vbforums.com/showthread....rot-Rendering) 
> 
> at Step7 StdExe (using STA-Dll) ... *crashes* when run outside or in vb6 IDE  when compiled to *p-code*.
> 
> Is there any reason for this since I don't have this problem when working in pure c/c++ applications in which i never come across *p-code* option while compiling in vc++ IDE like in vb6 IDE.


We are not allowed to call any vbRuntime-Functions from within the ThreadCallback until STA-instance-creation took place.
(that's the reason for using a *.tlb to call "other APIs").

And when in PCode-Mode - it seems not even the little trick with the *.tlb-based API-calls 
(this way avoiding Err.LastDllError interactions) is sufficient because the "Runtime is touched anyways"...

And yes, there's of course no PCode-option in C/C++ compilers - 
and, if "native-VB6-compilation" solves the issue in VB6 - why not keep it this way (it's the default).

Olaf

----------


## smkperu

> Dear Olaf,
> 
>   Thanks for the clarification.
>   I understood your explanation *even better* when I came across your another awesome pure 
> VB6 Threading-Tutorial (Mandelbrot-Rendering) at the link
> https://www.vbforums.com/showthread....rot-Rendering) at Step7 StdExe (using STA-Dll).
> 
> More or less both VB6ThreadingDirectCOM and VB6 Threading-Tutorial (Mandelbrot-Rendering) at the link
> https://www.vbforums.com/showthread....rot-Rendering) at Step7 StdExe (using STA-Dll) are same except
> ...



Hello Olaf,

    Now I understood that vb6 native code is the default option we can use in vb6.  
    You mean there is *no way to bypass runtime  in pcode* in threadcallback like using .tlb in native  code.
    Then for what *other purpose* is this *extra*  pcode option available in vb6 IDE.

Thanks

----------


## wqweto

> Then for what *other purpose* is this *extra*  pcode option available in vb6 IDE.


It's the other way around -- p-code compiled binaries were first intoduced in VB3 (or earlier) and it was in VB5 that native binaries become available.

They left p-code binaries for compatibility reasons, besides it makes no sense to remove it when thay *had* to support p-code interpreter for IDE debugging etc. interactions which cannot be replaced by any compiled code (incl. native compiled one).

cheers,
</wqw>

p.s. I already told you p-code multi-threading is not possible and now you know the reason -- the p-code interpreter is not thread-safe, not only the runtime is not thread-safe which can be circumvented with TLB usage.

----------


## smkperu

Dear wqweto and Olaf,

  Now I have understood better and mark the thread as resolved.

Thanks

----------

