Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <DllImport("user32.dll", CharSet:=CharSet.Auto)> Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) End Sub
  2. <DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> Private Shared Function GetRunningObjectTable(ByVal reserved As Int32) As IRunningObjectTable End Function
  3. <DllImport("ole32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=False)> Private Shared Function CreateItemMoniker(ByVal lpszDelim As String, ByVal lpszItem As String) As IMoniker End Function
  4. <DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> Private Shared Function CreateBindCtx(ByVal reserved As Integer) As IBindCtx End Function
  5.  
  6. Try
  7.  
  8. Dim ROTObject As Object = Nothing
  9. Dim runningObjectTable As IRunningObjectTable
  10. Dim monikerEnumerator As IEnumMoniker = Nothing
  11. Dim monikers(1) As IMoniker
  12.  
  13. runningObjectTable = GetRunningObjectTable(0)
  14. runningObjectTable.EnumRunning(monikerEnumerator)
  15. monikerEnumerator.Reset()
  16.  
  17. Dim numFetched As IntPtr = New IntPtr()
  18. While (monikerEnumerator.Next(1, monikers, numFetched) = 0)
  19. Dim ctx As IBindCtx
  20. ctx = CreateBindCtx(0)
  21.  
  22. Dim runningObjectName As String = ""
  23. monikers(0).GetDisplayName(ctx, Nothing, runningObjectName)
  24.  
  25. runningObjectName = runningObjectName.ToUpper
  26. If (Not runningObjectName.Equals("")) Then
  27. Dim runningObjectIns As Object = Nothing
  28. runningObjectTable.GetObject(monikers(0), runningObjectIns)
  29.  
  30. 'Check if object is a Catia object
  31. Try
  32. Dim catiaIns As INFITF.Application = Nothing
  33. catiaIns = DirectCast(runningObjectIns, INFITF.Application)
  34. ListCATIA.Items.Add(catiaIns.Windows.Count)
  35. Catch Exc As Exception
  36. MessageBox.Show(Exc.ToString())
  37. End Try
  38. End If
  39. End While
  40.  
  41. Catch Exc As Exception
  42. Throw Exc
  43. End Try
  44.  
  45. 1. Make a dll with two functions:
  46. HRESULT __stdcall CoMarshalToFile(IUnknown* punk, const char* const filePath)
  47. /* uses `::CreateStreamOnHGlobal`, `::CoMarshalInterface`, `::CoGetMarshalSizeMax`,
  48. and `::GetHGlobalFromStream` to marshal the IUnknown to the specified file.
  49. */
  50. HRESULT __stdcall CoMarshalFromFile(IUnknown** ppunk, const char* const filePath)
  51. /* uses `::CreateStreamOnHGlobal` and `::CoUnmarshalInterface` to marshal
  52. from the file to an IUnknown pointer.
  53. */
  54.  
  55. 2. In CATIA:
  56. Note: this only needs to be done on the development computer.
  57. Make a new "VBA projects" macro library.
  58. Add "declare" statements for:
  59. "LoadLibrary" (Windows API)
  60. "CoMarshalToFile" (DLL specified above)
  61. Add a function
  62. Public Function MarshalCatiaToFile _
  63. (marshalInstanceFilePath As String, _
  64. marshalDllFolder As String) As Long
  65.  
  66. MarshalCatiaToFile calls "LoadLibrary" to load the C++ DLL
  67. and then calls CoMarshalToFile (in DLL) to marshal the CATIA instance
  68. to a file.
  69.  
  70. Remove the macro library from CATIA's list of macro libraries.
  71.  
  72. 3. Create a file:
  73. "C:TempCatiaOnTheFlyCatScriptsOnTheFlyCatScript.catvbs"
  74. The file can be empty.
  75.  
  76. 4. In CATIA:
  77. Note: this must be done for *each* user of CATIA on *each* computer used.
  78. It may be possible to make this available to all users without individual
  79. setup required: it is saved in "FrameUserAliases.CATSettings"
  80. It may also be possible to reverse engineer the settings file and set up
  81. the needed data from outside CATIA.
  82.  
  83. Add "C:TempCatiaOnTheFlyCatScripts" as a new "Directories" macro library.
  84. Make the added library "current"
  85. Use "Tools --> Customize --> Commands --> Macros" to assign a
  86. "User Alias:" to the "OnTheFlyCatScript.catvbs" script file.
  87. Name the alias "ExecuteOnTheFlyCatScript".
  88. Remove the macro library from CATIA's list of macro libraries.
  89. Close CATIA at this point to force the changes to be saved.
  90.  
  91. 5. VB.net / C# program:
  92. Add the DLL (from step 1) and the CatVBA macro library (from step 2) as
  93. "Embedded Resource" to the project.
  94.  
  95. During program execution:
  96. Extract the DLL and macro library to an appropriate location.
  97. Load the DLL into session using "LoadLibrary".
  98. Create the file:
  99. "C:TempCatiaOnTheFlyCatScriptsOnTheFlyCatScript.catvbs"
  100.  
  101. The "OnTheFlyCatScript.catvbs" will be executed in CATIA. It
  102. uses CATIA.SystemService.ExecuteScript to execute the
  103. "MarshalCatiaToFile" function in the CatVBA macro library.
  104. Add method of choice to this file to indicate success/failure.
  105. I use a dialog box with the appropriate title.
  106.  
  107. To execute the "OnTheFlyCatScript.catvbs":
  108. Using the Windows API functions, get the window handle for the
  109. "Power Input" box at the bottom right of the "desired"
  110. CATIA window.
  111. Using the Windows API functions (*NOT* "SendKeys") send
  112. "c:ExecuteOnTheFlyCatScript" + {Enter} to the "Power Input".
  113. Wait for the "completion" signal from the script. If you used
  114. a dialog box, use the Windows API function to close it.
  115.  
  116. Assuming the script succeeded in marshaling the CATIA instance to
  117. a file, call the DLL function CoMarshalFromFile to get the CATIA
  118. instance.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement