ورود

View Full Version : سوال: اشکال در استفاده از dll



mesgar
پنج شنبه 27 فروردین 1388, 11:55 صبح
سلام
من با ویژوال استودیو 2005 یک پروژه MFC ساختم البته dll بود
بعد کد زیر رو توش نوشتم

extern "C" {
int GetInt
()
{
return 20;
}
}

تو قسمت def هم تابع رو اضافه کردم

بعد یه پروزه سی شارپ ساختم و کد زیر رو برای استفاده از dll بالا نوشتم


[DllImport("testLoad.dll", EntryPoint = "GetInt")]
public static extern int GetInt();
public Form1()
{
InitializeComponent();
MessageBox.Show(GetInt() + "");
}


برنامه رو اجرا کردم و درست انجام شد و مشکلی پیش نیامد
ولی وقتی برنامه اجرایی رو با تمام نیازهاش(همون dll) روی یه کامپیوتر دیگه بردم که تازه ویندوزش عوض شده بود و البته دات نت فریم ورک نیز روی نصب شده بود اجرا کردم ارور زیر داده شد
http://mesgar.persiangig.ir/error/error.JPG
وقتی ویژوال استودیو 2005 روی اون کامپیوتر نصب شد برنامه دوباره درست کار کرد!!!
کسی از دوستان هست که بدونه اشکال کار کجاست ؟؟؟

mesgar
پنج شنبه 27 فروردین 1388, 16:13 عصر
من در جستجویی که داشتم به جواب زیر رسیدم ولی دستوری که گفته باید بزنم رو نفهمیدم کجا باید بزنم(دستور مربوطه در داخل متن زیر رو درشت کردم)
Unhandled Exception: System.DllNotFoundException: Exception from HRESULT: 0x80131524

Since I had this problem a long time ago, and keep getting asked about it, I'll blog the answer. We originally got this problem when we had some C# code in an AppDomain, other than the default AppDomain, call some unmanaged code (C++). The unmanaged code would then perform a callback into C# at sometime in the future.
First you need to be aware of the Mixed DLL Loading Problem (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconMixedDLLLoadingProblem.asp) and linker warnings (http://support.microsoft.com/default.aspx?scid=kb;en-us;814472) you might have seen when compiling unmanaged code.
The System.DllNotFoundException will not normally occur in the above scenario if the default AppDomain is used. This is because when the unmanaged code makes a callback, it has no idea about the managed world (and AppDomain's), and so unless otherwise told, will invoke the callback into the default AppDomain. Hence the problem, if your original C# call to the unmanaged world was via another AppDomain, then the callback will be incorrect. The simplest way to solve the problem is to compile you unmanaged code with the Visual C++ compiler option (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vcrefEECOMCompilation.asp) "/clr:initialAppDomain". This should cause all calls from unmanaged threads to call back into the AppDomain that loaded the unmanaged code

ممنون میشم اگه کسی در این مورد کمکم کنه

Nima_NF
جمعه 28 فروردین 1388, 03:04 صبح
برنامه های win32 و یا MFC (در صورتی که dll از نوع MFC extension نباشد) نیازی به نصب .Net ندارند چون native هستند.

1- اما شما باید visualC++-runtime را در سیستم هدف نصب کنید (یا dll های آن ر با توجه به نسخه VC ا در کنار فایل اجرایی برنامه خود کپی کنید که کمتر از 3mb هستند)
اطلاعات بیشتر در این لینک ها( و البته باز هم جستجو کنید):
http://barnamenevis.org/forum/showpost.php?p=593459&postcount=6
http://barnamenevis.org/forum/showthread.php?t=103713

2- حتما dll را در حالت release کامپایل کنید.