WindowsServer2019下Php(php-7.4.3-Win32-vc15-x64) 调用C# .dll文件
生成.dll
类库选择:在VS2019中选择,这个是不包含.core的类库
类库属性中 > 使程序集COM可见
为程序集签名
测试.dll
namespace EncryptHelperWin
{
public class EncryptHelper
{
public string Write()
{
return "Hello World! WriteFunc.";
}
}
}
php相关配置
拷贝生成的.dll文件到php的ext目录
使用RegAsm.exe进行注册。
用gacutil.exe进行缓存
修改php.ini文件,添加extension=php_com_dotnet.dll
重起php,即可测试.dll功能。
编写测试用rsadll.php文件
<?php
$r=new COM("EncryptHelperWin.EncryptHelper");
$s=$r->Write();
echo $s;
?>
测试结果如下
【测试时产生 “未注册类” 错误进行的操作。如无此错误不需要此步】使用RegAsm.exe /regfile EncryptHelperWin.dll 生成注册表文件。导入注册表。
更新.dll文件
替换新版本的.dll文件
namespace EncryptHelperWin
{
public class EncryptHelper
{
public string Write()
{
return "Hello World! WriteFunc.Testdll.123.OK";
}
}
}
用gacutil.exe进行缓存
重启php。测试结果如下。
搜索查阅了很多资料,并动手进行了测试,最终成功。希望对您有所帮助。
参考内容:
- https://www.php.cn/php-ask-497189.html
- https://www.it1352.com/405511.html
- https://www.cnblogs.com/menyiin/p/4793292.html
- https://www.yisu.com/zixun/349169.html