简体   繁体   中英

Why do I get 0x80070002 error calling a C# COM object from VB when launched from C#?

I have a C# COM object in a dll that I have registered using regasm.exe. I do a createObject on the COM object and call methods on the object in a vbs script run with cscript.

If I run this on the command line it all works properly, creates the object calls the method via com.

cscript.exe c:\mypath\myvb.vbs argument

I now am trying to run this same command from C#. I use the System.Diagnostics.Process methods

ProcessStartInfo si = new ProcessStartInfo();
si.Filename = "cscript.exe";
si.Arguments = "c:\mypath\myvb.vbs argument";
Process exe = ProcessStart(si);
...

When I run this way I get the 0x80070002 error , which is basically a file not found error. I dont understand why it is different from C# to the command line.

Edit - More Info

I am running on a 64bit OS. The C# COM dll was built with "AnyCPU" . I used the 64bit regasm. The cscript I used was from c:\\windows\\system32 so it was the 64 bit version.

From the command line if I deliberately use the 32 bit version of cscript I also get the 0x80070002 error. This leads me to suspect the problem from c# is related, but I still don't get it.

"File not found" is not the first kind of error you'd expect in this scenario. But it is certainly possible, you have to register the assembly twice . Once with the 64-bit version of Regasm.exe so that the 64-bit registry keys are written. And again with the 32-bit version, it writes the keys to HKLM\\Software\\Wow6432Node, where 32-bit client programs search for keys.

Which is easy to overlook of course, you never mentioned doing this so that's a Big Red flag. You'd normally get "Class not registered", that didn't happen, maybe there was an earlier registration we don't know about. Like Visual Studio registering it, you'd normally always favor it doing that way because it prevents registry pollution. Simple changes to the project or file can then trigger "File not found". Forgetting the /codebase option when you ran the 32-bit version of Regasm is another way.

Best thing to do is just not guess at this. File not found errors are very easy to diagose with SysInternals' Process Monitor . You'll see cscript.exe searching for the file and not finding it. The name of the file gives you a very strong hint what the underlying cause can be. Look at the trace from the bottom up to avoid drowning in the data. And pre-emptively ensure you use both versions of Regasm.exe because you know that's necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM