C#调用BCB DLL

w88529593 2013-09-21 11:48:07
最近试着在C#中调用C++ builder 开发的DLL中的Form,但是程序只要一调用就出现以下异常:
“正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。”
这个错误,我按照网上的方法:在VS2010中按快捷键Ctrl+Alt+E,修改Managed Debuggin Assistants-> LoaderLock,将这个选项的选中状态去掉,然后运行的时候出现以下错误:
“System.StackOverflowException”类型的未经处理的异常出现在 System.Windows.Forms.dll 中。确保您没有无限循环或无限递归。”

C#部分的代码:
DLL函数的声明代码:
[DllImport("Project1.dll", EntryPoint = "ShowDLLForm", SetLastError = true,
CallingConvention=CallingConvention.StdCall)]
private static extern void ShowDLLForm();

C#调用代码:
private void button1_Click(object sender, EventArgs e)
{
ShowDLLForm();
}

-----------------------------------------------------------------------------
c++ builder代码:

extern "C" __declspec(dllexport) __stdcall void ShowDLLForm()
{
TForm1 *frm=new TForm1(Application);
frm->ShowModal();
delete frm;
}

这个DLL,我用C++ Builder写个Exe可以调用,没有任何问题,但是用C#就出现以上错误,请高手指点一下
...全文
524 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
w88529593 2013-09-26
  • 打赏
  • 举报
回复
我下载了VS2010 的SP1升级包安装了一下,现在 CB2010建立的DLL调用Form也没事了 周兄的代码可以正确执行,感谢周兄悉心指点,周兄的心态很让人佩服,以后一定多和周兄学习
周药师 2013-09-24
  • 打赏
  • 举报
回复
你用的win 2003 系统? 我这里只有 win7 32/64 的(均测试成功),无法在win 2003给你测试 你把你的demo 放到win7 下试试
周药师 2013-09-24
  • 打赏
  • 举报
回复
按这下面代码先试试,如何不行的话,留下你联系方式,发你的demo给我 我看看

//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "Unit2.h" //手动添加窗体TForm2
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
TForm2* For ;
extern "C" _declspec(dllexport) void __stdcall ShowForm();

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        return 1;
}
//---------------------------------------------------------------------------
void __stdcall ShowForm()
{
   For = new TForm2(NULL);
   For->Show();
}

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("Project2.dll", EntryPoint = "ShowForm")]
        static extern void ShowForm();

        /*
         解决方案资源管理器”里,
         右键项目名的属性。
         “生成”——“目标平台”
         下拉里选中“X86";即可
         */ 
        private void button1_Click(object sender, EventArgs e)
        {
            ShowForm();
        }     
    }
周药师 2013-09-23
  • 打赏
  • 举报
回复
刚才试试了下,你应该用的 64位操作系统吧? 这跟调用的环境有关的; C# 项目如下设置即可: 右键项目名的属性;“生成”-》“目标平台”; 下拉里选中“X86";即可
w88529593 2013-09-23
  • 打赏
  • 举报
回复
谢谢周兄指点,我用的是32位的xp系统,然后又用win 2003重新安装了开发环境,异常依旧
缘中人 2013-09-22
  • 打赏
  • 举报
回复
在c#里问问吧
w88529593 2013-09-22
  • 打赏
  • 举报
回复
多谢周兄指点,周兄的博客,我已拜读过 刚开始我以为是电脑系统或者开发环境的问题,我就换了个电脑,重新安装了VS2010和RAD2010,用的CB2010新建个DLL工程,不添加Form,随便导出个计算a,b之和的函数,C#调用没出现调用异常,可以返回a,b之和,接着我给DLL工程新添加个空白Form,但是unit1.cpp里面并不包含任何和Form有关的东西,既没有include Form的头文件,也没导出调用Form的函数,编译之后,在VS2010中设置断点,单步调试c#代码,运行到调用导出的计算a,b之和函数的时候,就出现异常了,此时只是添加了个Form,并没做其他的改动,于是从DLL工程中删除掉新添加的Form,编译之后,还是出现异常,真是崩溃透了…… 但是很是奇怪,如果将C#编译成exe后,直接打开exe点击调用DLL函数的测试按钮,不管DLL中是否包含有Form,都没有提示异常,都能正确调用 就是在vs2010中调试的时候,只要调用到导出的函数,就会出现异常,我在DllEntryPoint函数中,添加了DLL_PROCESS_ATTACH的时候,弹出个MessageBox,这个也能弹出,说明在vs2010调试时,DLL是被加载到内存了 如此简单的DLL,居然也会出现异常,实在想不到是哪儿会出现死循环了,一直找不到导致异常的原因 哪个兄台可以留个Email,我将工程发送到您的Email,您在那边VS中运行一下,看看是否会提示异常 小弟谢谢各位的指点
周药师 2013-09-22
  • 打赏
  • 举报
回复
这里有我的 做的例子 :C++Builder、C#、VC调用 C++Builder制作的DLL http://blog.csdn.net/zhouzhangkui/article/details/5815797

1,222

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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