asp.net 动态加载dll的问题

用c#动态加载dll,我在winform程序下已经实现了,可是把代码转到网页的后台代码中。
int hModule = LoadLibrary(_dllpath);加载dll是,老是返回0,不知道是何原因,难道还有其他玄机?望高手们给解答。
原winform的代码


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace dllImport
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
static extern bool FreeLibrary(int hModule);

delegate int GetBusinessData1(string inputData, ref StringBuilder outputdata);

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{ string _dllpath = @"G:\program\dllImport\Project2.dll";
string inputData = "1234";
StringBuilder AOutputData = new StringBuilder(213273);

int hModule = LoadLibrary(_dllpath);
if (hModule == 0)
return;
//得到指向Add函数的指针
IntPtr intPtr = GetProcAddress(hModule, "GetBusinessData");
GetBusinessData1 myfun = (GetBusinessData1)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(GetBusinessData1));
int iReturn = myfun(inputData, ref AOutputData);
MessageBox.Show(AOutputData.ToString());

}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}



...全文
455 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 github_22161131 的回复:
可能是iis的app pool配置的以64位运行,而你要加载的dll是32位的
错误显示,“找不到指定模块”
  • 打赏
  • 举报
回复
引用 7 楼 Saleayas 的回复:
       [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", CharSet=CharSet.Auto)]
        static extern int LoadLibrary(string lpLibFileName);
这个方法我明天试一试,不过我今天跟踪程序的时候,好像提示“找不到指定模块”,我看了一下那个dll,好像还调用了其他的dll,是不是因为这个原因。我也怀疑过是不是权限的原因,不过我把那个dll文件夹都添加了everyone权限,好像也还不是不行。
Saleayas 2014-12-18
  • 打赏
  • 举报
回复
       [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", CharSet=CharSet.Auto)]
        static extern int LoadLibrary(string lpLibFileName);
Im_Sorry 2014-12-18
  • 打赏
  • 举报
回复
用用虚拟路径
  • 打赏
  • 举报
回复
引用 4 楼 zzhaojin 的回复:
[quote=引用 2 楼 Saleayas 的回复:] 查看一下 last error , 看看是什么原因。
这个我看看[/quote] 竟然提示 “参数错误”,可是我传的路径是绝对路径,和winform的一样的呀。winform可以
  • 打赏
  • 举报
回复
引用 2 楼 Saleayas 的回复:
查看一下 last error , 看看是什么原因。
这个我看看
  • 打赏
  • 举报
回复
引用 1 楼 github_22161131 的回复:
可能是iis的app pool配置的以64位运行,而你要加载的dll是32位的
我修改了,可以允许32位的运行
Saleayas 2014-12-18
  • 打赏
  • 举报
回复
查看一下 last error , 看看是什么原因。
winnowc 2014-12-18
  • 打赏
  • 举报
回复
可能是iis的app pool配置的以64位运行,而你要加载的dll是32位的

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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