怎么得到用 Regsvr32注册dll的结果

冬天的小伙伴叫暖气 2014-01-07 11:33:16
在运行 regsvr32 C:\Users\wh\Desktop\ECCartoExtension.dll 后会弹出处理结果框
和提示错误框
由于注册的dll文件太多,
我在命令语句里添加:/s选择静默形式
现在我想获取是否注册成功的消息。然后保存起来。但是该怎么获取呢
for (int i = 0; i < list.Count; i++)
{
string num = list[i].ToString();//文件路径
int a = num.LastIndexOf(".");
string suffix = num.Substring(a + 1);//文件后缀名
if(suffix=="dll")
{
string commd = "regsvr32 \"" + num+"\"";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";//设定程序名
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;//关闭Shell的使用
p.StartInfo.RedirectStandardInput = true;//重定向标准输入
p.StartInfo.RedirectStandardOutput = true;//重定向标准输出
p.StartInfo.RedirectStandardError = true;//重定向错误输出
p.StartInfo.CreateNoWindow = false;//设置不显示窗口
p.Start();//启动
p.StandardInput.WriteLine(commd);//参数为DOS命令
p.StandardInput.WriteLine("exit");
dll++;
}

我在网上找到了判断是否注册成功的一个例子,但是由于是新手不懂的怎么修改
        [DllImport(@"C:\Users\wh\Desktop\啊啊\ECCartoExtension.dll")]
public static extern int DllRegisterServer();
int ah = DllRegisterServer();
if (ah >= 0)
{

//注册成功!

}

else
{

//注册失败

}

上面的[DllImport(@"C:\Users\wh\Desktop\啊啊\ECCartoExtension.dll")] 括号里面不能写变量,我怎么做才能获取到每注册一个dll文件就获取一个注册结果信息呢?
...全文
461 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 14 楼 zhao4zhong1 的回复:
改为
string commd = "regsvr32 \"" + num+"\"";
Process p = new Process();
p.StartInfo.FileName = "regsvr32.exe";//设定程序名
p.StartInfo.Arguments = "/s C:\\Users\\dview\\Desktop\\zhuc\\插件2\\GPSubPoints.dll";
p.StartInfo.UseShellExecute = false;//关闭Shell的使用
p.StartInfo.RedirectStandardInput = false;//重定向标准输入
p.StartInfo.RedirectStandardOutput = false;//重定向标准输出
p.StartInfo.RedirectStandardError = false//重定向错误输出
p.StartInfo.CreateNoWindow = false;//设置不显示窗口
int n;
p.Start();//启动
p.WaitForExit();//等待管理进程退出(DOS)
int pExit = -1;
if (p.HasExited)
{
  pExit = p.ExitCode;//==0成功,否则失败
  if (pExit == 0)
  {
      MessageBox.Show("注册成功");
  }
}

再试试?
赵老师真乃神人,可以了。感谢感谢
赵4老师 2014-01-09
  • 打赏
  • 举报
回复
改为
string commd = "regsvr32 \"" + num+"\"";
Process p = new Process();
p.StartInfo.FileName = "regsvr32.exe";//设定程序名
p.StartInfo.Arguments = "/s C:\\Users\\dview\\Desktop\\zhuc\\插件2\\GPSubPoints.dll";
p.StartInfo.UseShellExecute = false;//关闭Shell的使用
p.StartInfo.RedirectStandardInput = false;//重定向标准输入
p.StartInfo.RedirectStandardOutput = false;//重定向标准输出
p.StartInfo.RedirectStandardError = false//重定向错误输出
p.StartInfo.CreateNoWindow = false;//设置不显示窗口
int n;
p.Start();//启动
p.WaitForExit();//等待管理进程退出(DOS)
int pExit = -1;
if (p.HasExited)
{
  pExit = p.ExitCode;//==0成功,否则失败
  if (pExit == 0)
  {
      MessageBox.Show("注册成功");
  }
}

再试试?
赵4老师 2014-01-08
  • 打赏
  • 举报
回复
看效果请将a.bat的输出重定向到一个文本文件比如out.txt中, 即shell执行命令 a.bat >out.txt 然后读文件out.txt的内容。 或者你先在cmd窗口中执行a.bat看显示啥。
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
写一个批处理文件比如a.bat,然后运行该批处理。
regsvr32 /s C:\Users\wh\Desktop\ECCartoExtension.dll 
if errorlevel 1 echo ERROR!&goto :EOF
echo OK.
试试看。
这个运行后看不出来有什么效果
赵4老师 2014-01-08
  • 打赏
  • 举报
回复
写一个批处理文件比如a.bat,然后运行该批处理。
regsvr32 /s C:\Users\wh\Desktop\ECCartoExtension.dll 
if errorlevel 1 echo ERROR!&goto :EOF
echo OK.
试试看。
  • 打赏
  • 举报
回复
引用 12 楼 zhao4zhong1 的回复:
或者
Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("regsvr32 /s C:\\Users\\wh\\Desktop\\ECCartoExtension.dll")
procID = newProc.Id
newProc.WaitForExit()
Dim procEC As Integer = -1
If newProc.HasExited Then
    procEC = newProc.ExitCode //procEC==0成功,否则失败
End If
我按照这个修改了一下,但是它不管是否注册成功都都是注册成功。麻烦您再指导指导
if(suffix=="dll")
                {
                    string commd = "regsvr32 \"" + num+"\"";
                    Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";//设定程序名
                    p.StartInfo.Arguments = "";
                    p.StartInfo.UseShellExecute = false;//关闭Shell的使用
                    p.StartInfo.RedirectStandardInput = true;//重定向标准输入
                    p.StartInfo.RedirectStandardOutput = true;//重定向标准输出
                    p.StartInfo.RedirectStandardError = true;//重定向错误输出
                    p.StartInfo.CreateNoWindow = false;//设置不显示窗口
                    int n;
                    p.Start();//启动
                    p.StandardInput.WriteLine(@"regsvr32 /s C:\Users\dview\Desktop\zhuc\插件2\GPSubPoints.dll");
                    n = p.Id;
                    p.StandardInput.WriteLine("exit");//退出DOS,注释后DOS窗体需要手动关闭
                    p.WaitForExit();//等待管理进程退出(DOS)
                    int pExit = -1;
                    if (p.HasExited)
                    {
                        pExit = p.ExitCode;//proEC==0成功,否则失败
                        if (pExit == 0)
                        {
                            MessageBox.Show("注册成功");
                        }
                    }

                    //p.StandardInput.WriteLine(commd);//参数为DOS命令
                    dll++;
                }
                else if (suffix == "ttf")
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
直接调用dllregisterserver这个函数就可以了。不需要用regsvr32。
dll这个函数不是前面有[DllImport(@"C:\Users\wh\Desktop\啊啊\ECCartoExtension.dll")]吗?这个dll路径又不能写变量,怎么办版主大大
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
你可以通过调用loadlibrary getmodulehandle getprocedureaddress来动态调用dll,而避免使用attribute。
额,我是小白,刚刚实习不懂这个。只是想注册dll文件,因为我这里不用调用dll文件进行开发。这些dll文件都是公司以前封装好的,能说的详细点吗版主大大
  • 打赏
  • 举报
回复
引用 1 楼 wyd1520 的回复:
public static extern int DllRegisterServer(); ...你那个DLL里有这个方法??
不知道,这dll文件是别人给的测试用的
赵4老师 2014-01-08
  • 打赏
  • 举报
回复
或者
Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("regsvr32 /s C:\\Users\\wh\\Desktop\\ECCartoExtension.dll")
procID = newProc.Id
newProc.WaitForExit()
Dim procEC As Integer = -1
If newProc.HasExited Then
    procEC = newProc.ExitCode //procEC==0成功,否则失败
End If
赵4老师 2014-01-08
  • 打赏
  • 举报
回复
C#我也不熟,仅供参考:
Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("CMD /c a.bat >out.txt")
procID = newProc.Id
newProc.WaitForExit()
Dim procEC As Integer = -1
If newProc.HasExited Then
    procEC = newProc.ExitCode
End If
//读文件out.txt的内容
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
看效果请将a.bat的输出重定向到一个文本文件比如out.txt中,
即shell执行命令
a.bat >out.txt
然后读文件out.txt的内容。

或者你先在cmd窗口中执行a.bat看显示啥。


实在不好意思,我太菜了,不知道怎么获取这个输出的信息:“OK”或“ERROR”
threenewbee 2014-01-07
  • 打赏
  • 举报
回复
你可以通过调用loadlibrary getmodulehandle getprocedureaddress来动态调用dll,而避免使用attribute。
threenewbee 2014-01-07
  • 打赏
  • 举报
回复
直接调用dllregisterserver这个函数就可以了。不需要用regsvr32。
本拉灯 2014-01-07
  • 打赏
  • 举报
回复
public static extern int DllRegisterServer(); ...你那个DLL里有这个方法??

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧