CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Jul 2009
    Posts
    10

    Is OLE automation via Dll possible?

    Hi,

    I have made a standard windows dll in vb6 that is working, and can be called from a vc++6 application (using the trick described here [http://windowsdevcenter.com/pub/a/wi...l.html?page=1]).

    Now, I want to use implement COM client procedures into this dll so that it can instatiate and control another application on my computer, by using this dll.
    But when I include the code (shown below) into the dll, and try to use the dll, I get a MFC run time error from the calling vc++ application.
    Are there some settings I have forgotten when I built the .dll exe in order for it to accept also automation procedures? COM / Automation settings, e.g?

    Added code that causes .dll to crash:
    Dim AB As Object
    Set AB = CreateObject("Broker.Application")

    Any help appreciated,
    Thanks Razo

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is OLE automation via Dll possible?

    Please name the error you are getting.

  3. #3
    Join Date
    Jul 2009
    Posts
    10

    Re: Is OLE automation via Dll possible?

    Hi!

    At runtime a standard textbox appears saying: "Name_of_Calling_Program has stopped working". I Then get the message: "An unhandled win32 exception occured in Name_of_Calling_Program.exe [27452]" by the Visual Studio Just_In_Time Debugger (VisualStudio2008). I can then step into the debugger where I get lots of "unhandled exeption" errors..
    (I have not done any extra initialization w.r.t. OLE automation in the vc6++ application, I just call the .dll function, as if it was a normal .dll ).

    The debugger steps into the file "cmdtrg.cpp" in the function:
    AFX_STATIC BOOL AFXAPI _AfxDispatchCmdMsg(.......)
    switch (nSig)......
    case AfxSigCmd_b:
    // normal command or control notification
    ASSERT(CN_COMMAND == 0); // CN_COMMAND same as BN_CLICKED
    ASSERT(pExtra == NULL);
    bResult = (pTarget->*mmf.pfnCmd_b_v)(); <------------ Debugger points to this line.
    break;


    Here is the complete code that I used when I made the VB .dll:
    ------------------------------------
    ' .def file:
    NAME MathLib
    LIBRARY MathMod
    DESCRIPTION "Add-on Library of Mathematical Routines"
    EXPORTS DllMain @1
    vbfunksjon @2Dll code:
    ------------------------------------
    ' main .dll Code:
    Option Explicit
    Public Const DLL_PROCESS_DETACH = 0
    Public Const DLL_PROCESS_ATTACH = 1
    Public Const DLL_THREAD_ATTACH = 2
    Public Const DLL_THREAD_DETACH = 3

    Public Function DllMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean
    Select Case fdwReason
    Case DLL_PROCESS_DETACH
    ' No per-process cleanup needed
    Case DLL_PROCESS_ATTACH
    DllMain = True

    ' The next 3 lines is the Automation code that causes .dll to crash: <----------------
    Dim AB As Object
    Set AB = CreateObject("Broker.Application")
    AB.Analysis.Optimize (3)

    Case DLL_THREAD_ATTACH
    ' No per-thread initialization needed
    Case DLL_THREAD_DETACH
    ' No per-thread cleanup needed
    End Select
    End Function
    --------------------------

    Do I need to do any extra initialization in the calling vc++ application or in the VB. dll for this to work?

    Thanks,
    Ole
    Last edited by razo; August 5th, 2009 at 09:16 AM.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is OLE automation via Dll possible?

    It seems you better put the question up to the VB.NET section.
    Visual Studio 2008 has VB.NET which is different to VB6 which we deal here.

    Anyway I would suspect that the creation of the "Broker.Application" failed.
    After creation, try If AB is Nothing Then msgbox "creation failed"

  5. #5
    Join Date
    Jul 2009
    Posts
    10

    Re: Is OLE automation via Dll possible?

    Hi, no, I was using VB 6.0, (its just that I had VS2008 installed on my laptop also and its debugger chose to get activated)..

    As a test I tried to remove the lines of automation code and inserting:
    MsgBox ("Application failed") as you suggest. I then get the same (mfc) error, this time using the VC++6.0 debugger the error reads:
    "Unhandeled exception in TestDLL.exe (MSVBVM60.DLL): oxCoooooo5: Access violation"

    My main .dll code is now:
    ----------------
    Option Explicit

    Public Const DLL_PROCESS_DETACH = 0
    Public Const DLL_PROCESS_ATTACH = 1
    Public Const DLL_THREAD_ATTACH = 2
    Public Const DLL_THREAD_DETACH = 3

    Public Function DllMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean
    Select Case fdwReason
    Case DLL_PROCESS_DETACH
    ' No per-process cleanup needed
    Case DLL_PROCESS_ATTACH
    DllMain = True
    Case DLL_THREAD_ATTACH
    ' No per-thread initialization needed
    Set AB = CreateObject("Broker.Application")
    Case DLL_THREAD_DETACH
    ' No per-thread cleanup needed
    End Select
    End Function

    Public Function vbfunksjon(ByVal param As Integer) As Integer
    vbfunksjon = param * 2

    MsgBox ("Creation failed")

    End Function
    ----------
    The error comes only I try to call the "vbfunksjon". Loading the .dll is working fine..
    This must mean that the problem is not the automation code, but something more fundamental ?
    Thanks, Ole
    Last edited by razo; August 6th, 2009 at 09:52 AM.

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is OLE automation via Dll possible?

    My suggestion was meant a little different. I thought:
    Code:
    Case DLL_THREAD_ATTACH
    ' No per-thread initialization needed
    Set AB = CreateObject("Broker.Application")
    If AB Is Nothing Then MsgBox "Broker Creation failed"
    Try this to make sure the object exists.
    I'm not quite sure when the DllMain() is executet. Propably only when a function from this dll is called for the first time.
    I don't see any troublemaker in the vbfunksjon() you wrote.

  7. #7
    Join Date
    Jul 2009
    Posts
    10

    Re: Is OLE automation via Dll possible?

    I tried the code you suggest; it is not executed... I also tried to include the code after the case switch inside Dllmain just before End Function, but it was not executed.
    I also tried the same using just the MsgBox ("Failed") statement. None of these were executed neither.

    That was the reason why I included it in the function in the last post, because then I am sure it is executed. So, the code is called when I include it in the "vbfunksjon", giving the errormessage described; What can be wrong with the code?

    Regards Ole
    Last edited by razo; August 6th, 2009 at 01:08 PM.

  8. #8
    Join Date
    Mar 2002
    Location
    Izhevsk, Udmurtia, Russia
    Posts
    930

    Re: Is OLE automation via Dll possible?

    I recommend you to remove COM call such as CreateObject("Broker.Application") from DllMain fucntion. See Remarks from MSDN "DllMain Callback Function".

    "...Calling functions that require DLLs other than Kernel32.dll may result in problems that are difficult to diagnose. For example, calling User, Shell, and COM functions can cause access violation errors, because some functions load other system components. Conversely, calling functions such as these during termination can cause access violation errors because the corresponding component may already have been unloaded or uninitialized."
    With best wishes,
    Vita
    -----------------------
    Russian Software Development Network -- http://www.rsdn.ru

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is OLE automation via Dll possible?

    Yes, this seems plausible.
    Might be possible to replace the CreateObject(), though.
    In VB you can mostly set a reference to a dll or ocx which is responsible for the Object you want and then simply use
    Set AB = New BrokerObject

    But to further isolate the error do this:
    Don't create the Broker.Application at all. Comment out the Set AB = CreateObject(...
    (Certainly also comment out the If AB is Nothing...)
    THEN call your vbfunksjon()
    See if the error persists. As far as I can see your function does in no way use the object you have created and should therefore execute without error.
    If the error persists the reason is to be found elsewhere...

  10. #10
    Join Date
    Jul 2009
    Posts
    10

    Re: Is OLE automation via Dll possible?

    Hi, I tried to remove all the statements you mention. The function implementation now looks like:

    Public Function vbfunksjon(ByVal param As Integer) As Integer
    vbfunksjon = param * 2
    MsgBox ("Failed")
    End Function

    It still crashes. But if I remove the "MsgBox("Failed")"-line from the above code it works without error...
    Last edited by razo; August 7th, 2009 at 10:58 AM.

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is OLE automation via Dll possible?

    Well.
    Then the function implementation is not guilty.
    Now that you removed the msgbox statement (I'm not sure why it wouldn't work from a dll),
    put back in the object creation statement.
    The MsgBox("Failed") line is formulated wrong and might therefore crash:
    Either use the syntax Call MsgBox("Failed") or MsgBox "Failed" without the use of brackets.
    Your syntax calls it like a function without putting the return value anywhere. This might be the crashing reason.
    But you should have got an error during compile time, I think...

  12. #12
    Join Date
    Jul 2009
    Posts
    10

    Re: Is OLE automation via Dll possible?

    Hi, I included the object creation statement and tried substituting "MsgBox("Failed)" with Call MsgBox("Failed") and MsgBox "Failed", as you suggest, but I still get the same error message in both cases..

  13. #13
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is OLE automation via Dll possible?

    I admit, I'm puzzled about this, actually.
    What happens if you leave away the msgbox statemant completely? Still getting the crash?

  14. #14
    Join Date
    Jul 2009
    Posts
    10

    Re: Is OLE automation via Dll possible?

    Hi, if I leave the out the MsgBox statement so that the function looks like this:

    Public Function vbfunksjon(ByVal param As Integer) As Integer
    vbfunksjon = param * 2
    End Function

    then it functions correctly and returns the correct value..

  15. #15
    Join Date
    Apr 2009
    Posts
    394

    Re: Is OLE automation via Dll possible?

    The problem is, is that dlls do not normally have a visual interface. Even activex dlls don't normally have a visual interface.... UNLESS it is hosted by the calling applicationl. Thus, the dll does not know how to reference all the API's to create a window to display your message box.

    At least that is the way I remember it so I could be wrong but in this case I don't think so.



    Good Luck

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured