6
$\begingroup$

When coding with VBA functions in NETLink, I need the enum reference. It's ok to get the NETTypInfo with code:

LoadNETType[
  "Microsoft.Vbe.Interop.vbext_ComponentType"] // NETTypeInfo

but when to get the get the enum value, it fails

NETObjectToExpression[vbext_ComponentType`vbext _ct _StdModule]

because of conflicts in vbext _ct _StdModule coming up with the syntax UNDERLINE pattern, how to write the correct code in this case? Although I can get the enum value from other tools , I want use the enum names visually for reading.

$\endgroup$
  • $\begingroup$ On my machine the following code Needs["NETLink`"];LoadNETType[ "Microsoft.Vbe.Interop.vbext_ComponentType"] // NETTypeInfo doesn't work. ShouId I add some code (for example load a Assembly) ? $\endgroup$ – andre314 Nov 24 '18 at 11:10
  • $\begingroup$ @andre Please check up whether there be Microsoft.Vbe.Interop.dll registered at C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop. refering to docs.microsoft.com/en-us/visualstudio/vsto/…. $\endgroup$ – Jerry Nov 24 '18 at 11:48
  • $\begingroup$ As much fun as I had answering this question, I am voting to close on the grounds that the answer is easy to find in the documentation. $\endgroup$ – WReach Nov 24 '18 at 17:20
  • $\begingroup$ @WReach Thanks! I didnot read through the manual, it's solved. $\endgroup$ – Jerry Nov 24 '18 at 17:52
  • 1
    $\begingroup$ But, as LoadNETAssembly[] can use either the name of the file that contains the assembly or the name of the assembly itself (when the assembly is in the GAC, which is the case here), I have found the name of the assembly by trial and error (turns out to be "Microsoft.Vbe.Interop", but note that this name may be totally different from the name of the file). So after having evaluated LoadNETAssembly["Microsoft.Vbe.Interop"], I was able to reproduce OP's problem. $\endgroup$ – andre314 Nov 24 '18 at 23:13
7
$\begingroup$

Quoting from the Underscores in .NET Names subsection within the Calling .NET from the Wolfram Language section of the NETLink Tutorial:

.NET/Link maps underscores in type, method, property, and field names to "U".

So me must use:

vbextUComponentType`vbextUctUStdModule

The documentation does not mention it, but in the extremely unlikely event that this mangled name conflicts with another symbol, the one which is defined first takes precedence and the other is unavailable.

Self-contained Example

The question uses an example from the Microsoft Office interop library, which readers may not have. We can use the standard library class System._AppDomain to see the effect:

InstallNET[];
LoadNETType["System._AppDomain"]

UAppDomain`ReferenceEquals[Null, Null]
(* True *)

Just For Fun

A closer simulation of the original problem is hard to come by using just the standard library since underscores are discouraged .NET standard naming conventions. Just for fun, we can dynamically generate our own DLL that is closer to the example in the question:

First, we generate an assembly that has symbol names containing underscores:

Needs["NETLink`"]
InstallNET[];

$myDll = FileNameJoin @ {$TemporaryDirectory, "MyNs.dll"};

NETBlock @ Module[{provider, parameters, result}
, provider = NETNew["Microsoft.CSharp.CSharpCodeProvider"]
; parameters = NETNew["System.CodeDom.Compiler.CompilerParameters"]
; parameters@OutputAssembly = $myDll
; provider@CompileAssemblyFromSource[parameters,
    "namespace MyNs {
       public class vbext_ComponentType {
         public const string vbext_ct_StdModule = \"fake module\";
       }
     }" // List
  ]
];

With that ugliness aside, we can now load and access the problematic symbols:

LoadNETAssembly[$myDll];
LoadNETType["MyNs.vbext_ComponentType"];

vbextUComponentType`vbextUctUStdModule
(* "fake module" *)

Clean up:

UninstallNET[];
DeleteFile[$myDll]
$\endgroup$
  • $\begingroup$ Although _ could be substitued by U in codes, but still problem comes up in the value given to Property, i.e. @NumberFormatLocal = "0.00_" in which `_' should be removed or exceptions occur. $\endgroup$ – Jerry Nov 25 '18 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.