سؤال

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);
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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#

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top