I am getting following error when I try to use a function from DLL written in C++ in my C# UWP App. i can call same DLL in C# console application without any issues. I am not experienced in working with dll/dependencies. Really appreciate your help.
ERROR in C# UWP App:: System.DllNotFoundException: 'Unable to load DLL 'WOOF.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)'
Below Program works without any issues.
//Program.cs file - single file C# console program that imports WOOF C++ DLL. Function 'FnWoof() only returns 42
using System;
using System.Runtime.InteropServices;
namespace ConsoleAppCsharp1
{
class Program
{
[DllImport("WOOF", CallingConvention = CallingConvention.Cdecl)]
static extern int fnWoof();
static void Main(string[] args)
{
int reply = fnWoof();
Console.WriteLine(reply);
Console.WriteLine("Hello World!");
}
}
}
UWP app code- does not work. get unable to load DLL error listed above :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Runtime.InteropServices;
namespace UWPapp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class UnitTests : Page
{
[DllImport("WOOF.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int fnWoof();
public UnitTests()
{
this.InitializeComponent();
}
private void displayfromdll_Click(object sender, RoutedEventArgs e)
{
int reply=fnWoof();
result.Text = reply.ToString();
}
}
}
Woof DLL code
Woof.h
//Woof.h
#ifdef WOOF_EXPORTS
#define WOOF_API __declspec(dllexport)
#else
#define WOOF_API __declspec(dllimport)
#endif
extern "C" {
WOOF_API int _cdecl fnWoof(void);
}
Woof.cpp
//Woof.cpp
#include "pch.h"
#include "framework.h"
#include "Woof.h"
// This is an example of an exported function.
WOOF_API int fnWoof(void)
{
return 42;
}
dllmain.cpp
//dllmain.cpp
#include "pch.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Reproducible example: https://github.com/qaftab/Reproducible-example
User contributions licensed under CC BY-SA 3.0