I have been struggling with a very strange situation regarding loading my of a third party unmanaged c++ dll in my c# project. I have added the unmanaged dll in folder of my bin and in my c# code file i have imported the dll using Dll import attribute. The problem arises when i simply use dll name in Dll import attribute, when i specify complete path with dll name in dll import attribute i am able to load and call methods of c++ dll.
Problem is when i use dll name only in Dll import attribute it shows me following error message "Unable to load DLL 'C:\Users\i2v\Desktop\NetDEVSDK.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)."
I have my c++ dll in my bin folder and my project is set in x86 build mode(because c++ dll is build for x86).Also i have all c++ runtime redistributables installed on my machine.Please help me solve this issue.
My code -
public class MakeConnection
{
private bool ConnectDevice()
{
bool IsConnected = false;
try
{
Client.CLIENT_OpenSound(10);
ImporRequiredDLL.NETDEV_Init();
NETDEV_DEVICE_INFO_S pstDevInfo = new NETDEV_DEVICE_INFO_S();
IntPtr lpDevHandle = ImporRequiredDLL.NETDEV_Login(this.Camera.Get_Default_IP(), 80, this.Camera.Camera_UserName, this.Camera.Camera_Password, ref pstDevInfo);
if (lpDevHandle == IntPtr.Zero)
{
return IsConnected;
}
DevHandle = lpDevHandle;
IsConnected = true;
}
catch(Exception ex)
{
IsConnected = false;
ExceptionHandler.handleException(ex);
}
return IsConnected;
}
}
public class ImporRequiredDLL
{
[DllImport(@"NetDEVSDK.dll")]
public static extern Int32 NETDEV_StopVoiceCom(IntPtr lpVoiceComHandle);
[DllImport(@"NetDEVSDK.dll")]
public static extern IntPtr NETDEV_StartVoiceCom(IntPtr lpUserID, Int32 dwChannelID, IntPtr cbPlayDataCallBack, IntPtr lpUserData);
[DllImport(@"NetDEVSDK.dll")]
public static extern IntPtr NETDEV_Login(String szDevIP, Int16 wDevPort, String szUserName, String szPassword, ref NETDEV_DEVICE_INFO_S pstDevInfo);
[DllImport(@"NetDEVSDK.dll")]
public static extern Int32 NETDEV_Init();
}
public struct NETDEV_DEVICE_INFO_S
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 48)]
public byte[] szReserve;
}
User contributions licensed under CC BY-SA 3.0