2013-04-23 74 views
0

首先,我是C#的新手。我正在使用vs2010。c#中的Dllimport從

我想在我的C#項目中包含一個dll。我正在關注this。我做了dll。我想包括使用dllimport在C#項目中的這個DLL,但我得到錯誤,DLL不存在。我試圖給出完整路徑,或將.dll文件添加到當前項目中,但仍然沒有運氣。

請幫忙。

代碼:

[DllImport("MathFuncDll.dll")] 
public extern static double Add(double a, double b); 
double tt = Add(15, 10); 

錯誤:

System.Windows.Markup.XamlParseException was unhandled 
    Message='The invocation of the constructor on type 'WpfApplication1.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'. 
    Source=PresentationFramework 
    LineNumber=3 
    LinePosition=9 
    StackTrace: 
     at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) 
     at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 
     at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) 
     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) 
     at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc) 
     at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) 
     at System.Windows.Application.DoStartup() 
     at System.Windows.Application.<.ctor>b__1(Object unused) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
     at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
     at System.Threading.ExecutionContext.runTryCode(Object userData) 
     at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Windows.Threading.DispatcherOperation.Invoke() 
     at System.Windows.Threading.Dispatcher.ProcessQueue() 
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.Run() 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at WpfApplication1.App.Main() in E:\projects\development\vs_test\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: System.DllNotFoundException 
     Message=Unable to load DLL 'MathFuncDll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) 
     Source=WpfApplication1 
     TypeName="" 
     StackTrace: 
      at WpfApplication1.MainWindow.Add(Double a, Double b) 
      at WpfApplication1.MainWindow..ctor() in E:\projects\development\vs_test\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 31 
     InnerException: 
+0

示例是一個控制檯應用程序,你似乎在做wpf gui應用程序,它似乎認爲它是一個.net dll。 – 2013-04-23 16:32:26

+0

該DLL位於與編譯程序相同的位置正確嗎? – 2013-04-23 17:15:41

+0

@AlexeiLevenkov看看內部異常... – Yaur 2013-04-23 17:20:17

回答

0

愚蠢的我。

這是一個打字錯誤。雖然非常感謝依賴步行者。

這就是你如何學習。

3

的DLL可能需要一些其他的DLL不在路徑上。使用Dependency Walker來檢查。

您也可打某種這裏你有DLL,就像在描述這個case該文件夾的權限。

如果你比你的開發機以外的其他機器嘗試它,你可能需要安裝相應的Microsoft Visual C++再發行組件包。

+0

或者本身可能是在錯誤的位置的DLL(比如,在項目目錄中,而不是'BIN \ $(配置)'目錄下的可執行文件) – hvd 2013-04-23 16:38:59

+0

我只在一臺機器上。我做了.NET DLL(簡單地複製粘貼示例)並導入到另一個C++代碼中。依賴步行者也沒有給出任何錯誤。請思考一下? – Naresh 2013-04-24 12:40:32

2

將DLL添加到您的項目爲紐帶,並確保「複製到輸出目錄」設置爲「複製總是」或「複製,如果新」。

如果這樣做不起作用,請使用Dependency Walker來找出您所缺失的依賴關係,然後對這些依賴關係進行相同處理。

+0

我檢查依賴沃克。沒有錯誤:功能Addis: ?添加@ MyMathFuncs @ MathFuncs @@ SANNN @ Z入口點:0x0001113B – Naresh 2013-04-24 08:56:58