I am relative new in c# and I want to use this uDMX API, but when I try to add the uDMX.dll file I get this error: "The reference is not vaild or not supported" (see screenshot (in german))
- I can't add images yet -
Or am I doing it completely wrong?
I did not find another uDMX api or .dll file.
Hope for your help and Ideas.
You cant import an unmanaged win32 dll into a managed c# application like that, you need to specify the methods and parameters yourself.
The uDMX api exposes these methods
bool _stdcall Configure() ;
bool _stdcall Connected() ;
bool _stdcall ChannelSet(long Channel, long Value) ;
bool _stdcall ChannelsSet(long ChannelCnt, long Channel, long* Value) ;
bool _stdcall Info() ;
In order to use them in managed code, you need to provide the declaration, return value and parameters to .Net
This is done by marking a method as external and informing .Net where to find it, use the DllImport attribute found in the System.Runtime.InteropServices namespace.
using System;
using System.Runtime.InteropServices;
class uDMXTest
{
[DllImport("uDMX.dll")]
public static extern bool Configure();
public static void Main()
{
bool result;
result = Configure();
Console.WriteLine(result);
}
}
Example on howto provide a declaration of the Configure method, and howto use it.
You can declare the other methods in uDMX.dll the same way - note you may need to give the full path to uDMX.dll in the DllImport attribute
More info here : https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute?view=netframework-4.8
I compiled a static library in xcode, it contained just one .c source file:
int Get10()
{
return 10;
}
And this is how it's included in the .cs source file:
[DllImport ("__Internal",EntryPoint="UIRectFrameUsingBlendMode")]
private static extern int Get10 ();
But the return value of Get10() is '0'. Notice the EntryPoint is UIRectFrameUsingBlendMode, there is no reason for this, if I remove entrypoint the function Get10() is not found and an exception is thrown to that effect, though that probably has something to do with the problem. The issue is though I'm not sure what entrypoint to define as the lib is basically a single c source file with a function. I had tried using EntryPoint="Get10" but that resulted in:
System.EntryPointNotFoundException: Get10 at (wrapper
managed-to-native) ChicksnVixens.ChicksnVixensGame:Get10 () at
ChicksnVixens.ChicksnVixensGame..ctor () [0x000a0] in
/ChicksnVixens/ChicksnVixensGame.cs:36
The code is being compiled and run in MonoDevelop in the iPhone simulator.
Any ideas why?
The EntryPoint should, if present, match the name of your native method. This is useful if you want to rename the method on the managed side (otherwise simply do not use it).
My guess is that your library is not linked correctly or that the Get10 symbol is not visible (e.g. static in C). In such case the runtime should throw an EntryPointNotFoundException.
Both 'nm' and 'otool' can list what's exported from your static library (i.e. you should see Get10, otherwise it's a build issue on your native library). See 'man' on both tools to get the lists.
I am having some strange problem. I have written a small module in VC++ using OpenCV.
It works fine. The code aggregates feed from the CCTV camera connected to the USB port.
however, I had to write the rest of my application in C#, so I made a DLL of the VC++ code and called the VC++ method from C#.
Now, I have ended up getting an error
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
Can anyone please suggest me any solution to this. Is there any Access Violation while accessing it in a managed code?
If TrackBlob returns a string, you should be able to define your dllimport as such:
[DllImport("Tracking.dll", EntryPoint = "TrackIt")]
public extern static string TrackBlob();
and skip trying to marshal it.
By returning it as an IntPtr, you're trying to get a pointer into memory owned by the unmanaged DLL... returning it as a string will return a copy of the string for you to work with.
Let me know if that works!
James
* Edit *
Try one of these:
[DllImport("Tracking.dll", EntryPoint = "TrackIt")]
public extern static [MarshalAs(UnmanagedType.BStr)] string TrackBlob();
or
[DllImport("Tracking.dll", EntryPoint = "TrackIt")]
public extern static [MarshalAs(UnmanagedType.AnsiBStr)] string TrackBlob();
Check out this MSDN link on string marshalling:
http://msdn.microsoft.com/en-us/library/s9ts558h.aspx
I'm developing an application that will allow users to call external code from both managed and native .dlls. The users will be able to specify what library/method/function to call at runtime (it will be stored in a configuration file).
I know how to do this using pinvoke for native libraries if I know what dll/function I want to call at compile time, but I can't find any information on how to do this at runtime.
Essentially what I'd like to do is call a method:
int result = ExecuteNativeFunction("someLibrary.dll", "foo");
and have it do something equivalent to:
[DllImport("someLibrary.dll")]
static extern int foo();
...
int result = foo();
Would this be what you are looking for? Using System.Reflection.Emit, you can dynamically compile code that defines a new PInvoke method. See the class DllRegServer in the linked file for details.
I have a dll which comes from a third party, which was written in C++.
Here is some information that comes from the dll documentation:
//start documentation
RECO_DATA{
wchar_t Surname[200];
wchar_t Firstname[200];
}
Description:
Data structure for receiving the function result. All function result will be
stored as Unicode (UTF-8).
Method:
bool recoCHN_P_Name(char *imgPath,RECO_DATA *o_data);
Input:
char * imgPath
the full path of the image location for this
function to recognize
RECO_DATA * o_data
data object for receiving the function
result.
Function return:
True if Success, otherwise false will return.
//end documentation
I am trying to call the recoCHN_P_Name from my C# application. To this end, I came up with this code:
The code to import the dll:
public class cnOCRsdk
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct RECO_DATA{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=200)]
public string FirstName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
public string Surname;
}
[DllImport(#"cnOCRsdk.dll", EntryPoint="recoCHN_P_Name")]
public static extern bool recoCHN_P_Name(byte[] imgPath, RECO_DATA o_data);
}
The code to call the function:
cnOCRsdk.RECO_DATA recoData = new cnOCRsdk.RECO_DATA();
string path = #"C:\WINDOWS\twain_32\twainrgb.bmp";
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytes = encoding.GetBytes(path);
bool res = cnOCRsdk.recoCHN_P_Name(bytes, recoData);
And the error I'm getting is
""Unable to find an entry point named 'recoCHN_P_Name' in DLL 'cnOCRsdk.dll'."
I'm suspecting that I'm having an error in converting a type from C++ to C#. But where exactly ... ?
First make sure the function is actually exported:
In the Visual Studio Command Prompt, use dumpbin /exports whatever.dll
C# doesn't support C++ name mangling and you either need to declare the C++ functions with
extern "C" {...}
(may not an option if they're from a third party), or call the mangled name directly if you can get it to work. It may be easier to get the third party to provide a non-mangled interface to the functionality.
Solved - at least to the point where the program does not break and actually returns me a bool value.
The key, I guess, was to specify the entry point as the 'mangled' name
[DllImport(#"cnOCRsdk.dll", EntryPoint="?recoCHN_P_Name#CcnOCRsdk##QAE_NPADPAURECO_DATA###Z")]
public static extern bool recoCHN_P_Name(ref string imgPath, ref RECO_DATA o_data);
After that I got some other errors but the 'unable to find entry point' went away.
I solved the same problem in these steps:
step 1) If you program your custom DLL in C++ using Visual studio,then at the property page of your project set the Common Language Runtime Support (/clr)parameter to Common Language Runtime Support (/clr).
step 2) To function deceleration in .h file use __declspec(dllexport) keyword like below:
__declspec(dllexport) double Sum(int a,int b);
step 3) Build and export DLL file, then use the Dependency Walker software to get your function EntryPoint.
step4) Import DLL file In the C# project and set EntryPoint and CallingConvention variable like below:
[DllImport("custom.dll", EntryPoint = "?Sum##YAXHHHHHHNNN#Z", CallingConvention = CallingConvention.Cdecl)]
public static extern double Sum(int a,int b);
I'd write a wrapper using C++/CLI. This wrapper will be able to include the .h files and link to the .lib files you got from the third party vendor. Then it is both easy and safe to write a managed interface for your C# program.
Correct EntryPoint string could be found in ".lib" file that comes along with main unmanaged dll.
We had this problem when we want to access to DB and solved it by changing EF core to EF 6.4.4
It may be you have a problem like this and need to change or downgrade your version of EF (If you used EF)
We had this problem .we change EntityFramework.core to EntityFrameWork 6.4.4 and after that the program worked fine. you most change you're Framework Version.
you may get this error due to string marshalling mismatch between DLL and your application . for example, one is using ANSI and the other is unicode.
you can try something like this:
[DllImport("yourDLL.dll", CharSet = CharSet.Unicode )]
public static extern String YourFunction(String name);
checkout HERE for a list of other possible reasons.
You could try using the unmangled name while specifying a CallingConvention in the DllImport