2013-04-23 77 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