1

How to register .ocx file with (regsvr32) from code in C# ?

i use below code but says :

The system cannot find the file specified

System.Diagnostics.Process.Start("regsvr32 " + ocxfile_path);
SajjadZare
  • 2,487
  • 4
  • 38
  • 68

3 Answers3

4

When you call Process.Start you need to specify arguments separately.

System.Diagnostics.Process.Start("regsvr32 ", ocxfile_path);

All I changed is + to ,. If ocxfile_path has spaces in it, you need to enclose it in quotes.

Michael Eakins
  • 4,149
  • 3
  • 35
  • 54
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • @SajjadZare, the original `Process.Start(string)` method you specified in your question only takes the file name of the executable you want to run. It should not get arguments. You need to specify arguments to the process as the second parameter to `Process.Start(string, string)`. – Samuel Neff Aug 25 '11 at 05:32
2

Although it does not strictly answer the question, there is also a way to register a DLL without using regsvr32 which is an external program. There is an example of such a code here: Register and Unregister COM DLLs from C#

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
0

Although not a strict answer to the question, I find it superior to what has been posted thus far.

Here's how to do it in managed code (albeit C++/CLI):

typedef HRESULT (WINAPI *DllRegisterServer)(void);

namespace Utility
{
    ref class Win32
    {
        static bool RegisterDLL(String^ pathToDll)
        {
            pin_ptr<const wchar_t> nativePathToDll = PtrToStringChars(pathToDll);
            HMODULE hComDll = LoadLibraryW(nativePathToDll);

            if (hComDll  == nullptr)
                return false;

            DllRegisterServer regSvr32 = (DllRegisterServer)GetProcAddress(hComDll, "DllRegisterServer");

            if (regSvr32 == nullptr)
                return false;

            if (regSvr32() != S_OK)
                return false;

            return FreeLibrary(hComDll) == TRUE;
        }
    };
}

Error handling is left as an exercise for the reader.

markf78
  • 597
  • 2
  • 7
  • 25