Results 1 to 14 of 14

Thread: [RESOLVED] [help]How to get an application object from the process id?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Resolved [RESOLVED] [help]How to get an application object from the process id?

    Several Excel had been startup. How can I get the Excel object from the specify process id?

    The function GetObject([path name] [, class name]) can only return the first Excel object.

    But I want to get the second or third ... 's object.

    Thank you very much.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [help]How to get an application object from the process id?

    That's going to be tough, if not impossible. The opposite is rather easy. But you're trying to get a reference to an Application's object, when you had nothing to do with the original instantiation.

    That object is going to be in a different memory address space to which you don't have access. You mentioned GetObject, but I'm not even clear what you're going to do with that.

    If I understand what you're trying to do (basically automate a copy of Excel that was independently fired up and already in memory), I'll be quite surprised if you get there.

    For this to work, you'd have to be the one who initially started that copy of Excel, and instantiated it's application object.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Hi Elroy,

    Thank you for your reply.

    The whole process is :

    1) I create a ActiveX DLL under VB6.0 in computer A, then used it in VBA project. It can work properly.

    2) When I copy the workbook and DLL to another computer B, it can't work properly, because the user account of B don't have admin privilege, so regsvr32 can't be executed.

    3) For solving this issue, I followed this article : Reg Free COM component.

    4) In computer B, 3) can't be worked as well, because the user account can't write anything into the folder of Microsoft office.

    5) So I want to copy the Excel.exe to other folder, then use the Shell("path", [windows state]) command to start it. But the if only return the PID.

    6) How can I get the Excel Object by PID?

    Thank you for your help.

    Best Regards,

    Sniperhgy

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [help]How to get an application object from the process id?

    if you had a known workbook open in the new instance of excel, you could use getobject on the workbook, which would return the correct instance, unless some other instance had the same workbook open

    So I want to copy the Excel.exe to other folder, then use the Shell
    surely that would be fraught with other poblems
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Quote Originally Posted by westconn1 View Post
    if you had a known workbook open in the new instance of excel, you could use getobject on the workbook, which would return the correct instance, unless some other instance had the same workbook open

    surely that would be fraught with other poblems
    Hi westconn1,

    Thank you for your reply. But in my opinion, if you start several Excels, even if use GetObject("the excel file you want to process"), it will return the first Object of Excel, because the pointer of ROT always point to the first one.

    And the shell statement only return a PID of the process, don't return the object.

    Best Regards,

    Sniperhgy

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [help]How to get an application object from the process id?

    Hi gyhu,

    Okay, here's the way I'd address the problem. As I understand it, your problem is that you can't get an ActiveX DLL registered on computer B. Therefore, you're struggling to use it. Basically, I see two solutions:

    1) Why not just pull all the ActiveX DLL's code into your Excel VBA code? I can't imagine why it needs to stay in a DLL. Hmmm, well, other than you possibly being worried about closed-code issues (i.e., keeping your source code private).

    2) If I had to keep the DLL, I'd use some methods developed by The Trick to use the DLL. Specifically, those methods can be found in a lengthy tutorial I developed. If you go to this post of the tutorial, you'll see an attachment named ActiveX_DLL_Example.zip. And then, within that example, looking at EXE_Module1.bas, you'll find a procedure named NewObjectFromActivexDll. That function will allow you to instantiate objects (with their corresponding object variable references) without any need for registering your DLL. Be sure to look at the entire example to understand how to fully use it, including the call to UnloadActivexDll when you're done with the DLL's various classes. That approach should give you a way to use your DLL from Excel's VBA code without any need for registration or administrative rights.

    Good Luck,
    Elroy

    EDIT1: Notice that my solutions entirely short-circuits your outlined points from post #3, from point #3 forward (points #3, #4, #5, #6). In other words, now that I more fully understand your problem, I've proposed a solution that has nothing to do with getting an object from a running process. In other words, I've solved the problem you outlined in point #2, so that the rest of your problems are obviated.
    Last edited by Elroy; Mar 25th, 2018 at 09:07 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Quote Originally Posted by Elroy View Post
    Hi gyhu,

    Okay, here's the way I'd address the problem. As I understand it, your problem is that you can't get an ActiveX DLL registered on computer B. Therefore, you're struggling to use it. Basically, I see two solutions:

    1) Why not just pull all the ActiveX DLL's code into your Excel VBA code? I can't imagine why it needs to stay in a DLL. Hmmm, well, other than you possibly being worried about closed-code issues (i.e., keeping your source code private).

    2) If I had to keep the DLL, I'd use some methods developed by The Trick to use the DLL. Specifically, those methods can be found in a lengthy tutorial I developed. If you go to this post of the tutorial, you'll see an attachment named ActiveX_DLL_Example.zip. And then, within that example, looking at EXE_Module1.bas, you'll find a procedure named NewObjectFromActivexDll. That function will allow you to instantiate objects (with their corresponding object variable references) without any need for registering your DLL. Be sure to look at the entire example to understand how to fully use it, including the call to UnloadActivexDll when you're done with the DLL's various classes. That approach should give you a way to use your DLL from Excel's VBA code without any need for registration or administrative rights.

    Good Luck,
    Elroy

    EDIT1: Notice that my solutions entirely short-circuits your outlined points from post #3, from point #3 forward (points #3, #4, #5, #6). In other words, now that I more fully understand your problem, I've proposed a solution that has nothing to do with getting an object from a running process. In other words, I've solved the problem you outlined in point #2, so that the rest of your problems are obviated.
    Thank you very much. I'll take a shot on your suggestion.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Quote Originally Posted by Elroy View Post
    Hi gyhu,

    1) Why not just pull all the ActiveX DLL's code into your Excel VBA code? I can't imagine why it needs to stay in a DLL. Hmmm, well, other than you possibly being worried about closed-code issues (i.e., keeping your source code private).
    Yes, you're right. My manager asked us: Don't let client see our source code.

    As you know, the VBA project's protection is very weak, just like a toy. So I have to write all the codes into a DLL file.

    Thank you.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [help]How to get an application object from the process id?

    Also, don't forget that, by using VB6 DLLs with the Excel VBA, you're not going to be able to use these DLLs with any Excel 64-bit version. Personally, I'd strongly consider placing all the code into the VBA, and just use a very long (and strong) password. I'll grant you that p-code protection isn't as strong as binary code, but it's stronger than you think. I've never deliberately tried to hack/disassemble any modern Excel VBA code, but I've occasionally snooped at utilities, and I've never really seen anything that looks all that great. Most of it looks like smoke-and-mirrors.

    What in the world are you doing where you're worried about a level of protection that a very long password and good copyright notifications wouldn't be sufficient? If it's something that severe, I'd think that an installation procedure requiring administrative rights wouldn't be that big of an ask. Also, if you're wanting to stay relevant to the MS-Office (including Excel) world, you've got to address 64-bit versions. Therefore, regrettably, your entire project is in the wrong language, and should be some C derivative, or (yuck) .Net, or again the VBA.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [help]How to get an application object from the process id?

    I don't have a solution for 64-bit Excel yet, but I outlined what's required in:

    Reg-Free COM at runtime via Microsoft.Windows.ActCtx


    I've updated that utility and included it here with a demo.

    First compile DLLAsm. Then compile TabParse, creating a DLL. Then run DLLAsm.exe against TabParse.dll to create the TabParse.manifest file. You can do that via a command-line run, by drag-drop, or by copy-paste (see the readme.txt for DLLAsm).

    Once that is done unregister the DLL by drag-dropping it onto the DLLUnreg.vbs icon.

    At that point you can open the Text.xls document and run the macro ("DoTheTest") I have predefined there.


    Name:  Diagram.png
Views: 3893
Size:  8.0 KB


    Code:
    Public Sub DoTheTest()
        Dim ParsedData As Variant
        Dim Row As Long
        Dim Col As Integer
    
        With CreateObject("Microsoft.Windows.ActCtx")
            .Manifest = Application.ActiveWorkbook.Path _
                      & "\TabParse\TabParse.manifest"
            With .CreateObject("TabParse.Parser")
                ParsedData = _
                    .TabularParseFile(Application.ActiveWorkbook.Path & "\Test.data", _
                                      NullExpr:="*", _
                                      ColNameHeaderRows:=3)
            End With
        End With
        For Col = 0 To UBound(ParsedData, 1)
            ActiveSheet.Columns(Col + 1).ColumnWidth = 15
        Next
        'No attempt made here to color the header rows, collapse header cells in
        'the first row, align headers centered, etc.
        For Row = 0 To UBound(ParsedData, 2)
            For Col = 0 To UBound(ParsedData, 1)
                ActiveSheet.Cells(Row + 1, Col + 1).Value2 = ParsedData(Col, Row)
            Next
        Next
    End Sub
    Attached Files Attached Files

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Quote Originally Posted by Elroy View Post
    Also, don't forget that, by using VB6 DLLs with the Excel VBA, you're not going to be able to use these DLLs with any Excel 64-bit version. Personally, I'd strongly consider placing all the code into the VBA, and just use a very long (and strong) password. I'll grant you that p-code protection isn't as strong as binary code, but it's stronger than you think. I've never deliberately tried to hack/disassemble any modern Excel VBA code, but I've occasionally snooped at utilities, and I've never really seen anything that looks all that great. Most of it looks like smoke-and-mirrors.

    What in the world are you doing where you're worried about a level of protection that a very long password and good copyright notifications wouldn't be sufficient? If it's something that severe, I'd think that an installation procedure requiring administrative rights wouldn't be that big of an ask. Also, if you're wanting to stay relevant to the MS-Office (including Excel) world, you've got to address 64-bit versions. Therefore, regrettably, your entire project is in the wrong language, and should be some C derivative, or (yuck) .Net, or again the VBA.

    Good Luck,
    Elroy
    Hi Elroy,

    We can do some test, please upload a file with the strong password, let's see whether I can unprotect it ^_^

    Best Regards,

    Sniperhgy

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [help]How to get an application object from the process id?

    Ok, that'll be good. Attached is an Excel file. It's got a single message box in code that's in the Workbook_Open event. Nothing else.

    Best Regards,
    Elroy
    Attached Files Attached Files
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Quote Originally Posted by Elroy View Post
    Ok, that'll be good. Attached is an Excel file. It's got a single message box in code that's in the Workbook_Open event. Nothing else.

    Best Regards,
    Elroy
    Hi Elroy,

    Your source code is
    Code:
    Option Explicit
    
    Private Sub Workbook_Open()
        ' Code written by Elroy.
        If True Then MsgBox "Hello World.  Can you show me this source code?"
    End Sub

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    64

    Re: [help]How to get an application object from the process id?

    Quote Originally Posted by dilettante View Post
    I don't have a solution for 64-bit Excel yet, but I outlined what's required in:

    Reg-Free COM at runtime via Microsoft.Windows.ActCtx


    I've updated that utility and included it here with a demo.

    First compile DLLAsm. Then compile TabParse, creating a DLL. Then run DLLAsm.exe against TabParse.dll to create the TabParse.manifest file. You can do that via a command-line run, by drag-drop, or by copy-paste (see the readme.txt for DLLAsm).

    Once that is done unregister the DLL by drag-dropping it onto the DLLUnreg.vbs icon.

    At that point you can open the Text.xls document and run the macro ("DoTheTest") I have predefined there.


    Name:  Diagram.png
Views: 3893
Size:  8.0 KB


    Code:
    Public Sub DoTheTest()
        Dim ParsedData As Variant
        Dim Row As Long
        Dim Col As Integer
    
        With CreateObject("Microsoft.Windows.ActCtx")
            .Manifest = Application.ActiveWorkbook.Path _
                      & "\TabParse\TabParse.manifest"
            With .CreateObject("TabParse.Parser")
                ParsedData = _
                    .TabularParseFile(Application.ActiveWorkbook.Path & "\Test.data", _
                                      NullExpr:="*", _
                                      ColNameHeaderRows:=3)
            End With
        End With
        For Col = 0 To UBound(ParsedData, 1)
            ActiveSheet.Columns(Col + 1).ColumnWidth = 15
        Next
        'No attempt made here to color the header rows, collapse header cells in
        'the first row, align headers centered, etc.
        For Row = 0 To UBound(ParsedData, 2)
            For Col = 0 To UBound(ParsedData, 1)
                ActiveSheet.Cells(Row + 1, Col + 1).Value2 = ParsedData(Col, Row)
            Next
        Next
    End Sub
    Hi dilettante,

    Thank you for your demo, now I can load the reg-free dll in excel without write a manifest file for excel.exe.

    Best Regards,

    Sniperhgy

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