Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
17060 Discussions

Console for DLL in non-console forms application

jimdempseyatthecove
Honored Contributor III
414 Views

Have you ever been in the position of having written a DLL that is incorporated into a Windows Forms application that has no console window....
... and all the diagnostic trace code requires console output.

While it is simple to create a console inside your code of the DLL, what do you do when the error your are trying to trace out occurs at DLL load time prior to entry into Main of your forms application?

I recently had come upon this situation. My DLL depended upon a 3rd party DLL that had load issues. Fortunately, their DLL had an option switch, enabled by environment variable, that would dump out diagnostic information to the console. Unfortunately when there is no console, this information gets lost.

The solution is to create the console at the time of LoadDLL that is called inside the runtime system initialization phase at program load time. To do this you add a startup routine, typically referred to as DllMain.

function DllMain (hInstDLL, fdwReason, lpReserved)
!DEC$ ATTRIBUTES STDCALL, DECORATE, DLLEXPORT, ALIAS:"DllMain" :: DllMain
    USE IFWINTY
    USE Kernel32
    
    IMPLICIT NONE
    integer(BOOL) :: DllMain
    integer(HANDLE), intent(IN) :: hinstDLL
    integer(DWORD), intent(IN) :: fdwReason
    integer(LPVOID), intent(IN) :: lpReserved
    
    character(500) :: eVar
    logical, save :: FirstTime = .true.
    
    integer(BOOL) :: b
    
    if(FirstTime) then
        FirstTime = .false.
        b = AllocConsole()
        write(*,*) "***************************"
        CALL GET_ENVIRONMENT_VARIABLE("MIC_LIBRARY_PATH",eVar)
        write(*,*) "MIC_LIBRARY_PATH=",TRIM(eVar)
        CALL GET_ENVIRONMENT_VARIABLE("MIC_LD_LIBRARY_PATH",eVar)
        write(*,*) "MIC_LD_LIBRARY_PATH=",TRIM(eVar)
        write(*,*) "***************************"
        flush(6)
        DllMain = 1
    else
        DllMain = 0
    endif
end function DllMain

In my situation, an additional environment variable had to be set to enable the other DLL's trace output. This worked to get the information we needed to start the investigation of offload failure.

Jim Dempsey

 

0 Kudos
0 Replies
Reply