首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

C#调用BCB DLL解决方案

2013-09-28 
C#调用BCB DLL最近试着在C#中调用C++ builder 开发的DLL中的Form,但是程序只要一调用就出现以下异常:“正试

C#调用BCB DLL
最近试着在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#就出现以上错误,请高手指点一下
c++ c#异常
[解决办法]
按这下面代码先试试,如何不行的话,留下你联系方式,发你的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();
        }     
    }

热点排行