How to run regasm.exe from a C++ program?

2019-08-15 06:48发布

I want to write a program which runs regasm.exe to create a tlb file programatically.

How can I do this??

Ur help is greatly appreciated...

Thanks in advance.

2条回答
爷的心禁止访问
2楼-- · 2019-08-15 07:13

You have to use the CreateProcess() function to run the command line like "fullPathToRegasm /somekeys filename".

The main problem is to detect the regasm location - use GetCORSystemDirectory() function for that. First use first LoadLibrary() to load the mscoree.dll, then call GetProcAddress() to locate the GetCORSystemDirectory() function, then call the function.

This will get you the root of .NET installation. regasm is usually located in the same subpath of the installation root on any given machine so you can safely combine the detected root with the subpath and this will be a valid regasm location.

查看更多
神经病院院长
3楼-- · 2019-08-15 07:23

Since NET is a side-by-side installation you might have multiple versions of regasm.exe in your system, and similarly some version are just updates so you might have it missing from some locations you would expect finding.

I have spent hours searching for the best way to do this, since there are also several atempts to get to the right point (registry, unmanaged code, Environment, etc) which greatly varies on the NET version. And each Windows also version brings his own NET version.

None of those above were stable enough across systems for my taste, so I found my own way: just searching all available regasm.exe files and check which is the newest version. As simple as that.

It might be a matter of opinion, but if you felt my answer could have higher success, thumbs up!!

// Scan for ALL regasm.exe available.
string netdir = Environment.GetEnvironmentVariable("WINDIR") + "\\Microsoft.NET\\";
string[] filelist = System.IO.Directory.GetFiles(netdir, "regasm.exe", System.IO.SearchOption.AllDirectories);

// Find the NEWEST regasm.exe available.
string newestFilePath = string.Empty;
FileVersionInfo currFileVersion = null;
FileVersionInfo newestFileVersion = null;
foreach (string currFilePath in filelist) {
    currFileVersion = FileVersionInfo.GetVersionInfo(currFilePath);
    if (newestFileVersion == null) {
        newestFilePath = currFilePath;
        newestFileVersion = currFileVersion;
    } else if ((currFileVersion.FileMajorPart >= newestFileVersion.FileMajorPart) &&
        (currFileVersion.FileMinorPart >= newestFileVersion.FileMinorPart) &&
        (currFileVersion.FileBuildPart >= newestFileVersion.FileBuildPart) &&
        (currFileVersion.FilePrivatePart > newestFileVersion.FilePrivatePart)) {
        newestFilePath = currFilePath;
        newestFileVersion = currFileVersion;
    }
}
查看更多
登录 后发表回答