DllImport issue in c sharp
-
Hello everyone, I'm making a program that works on some lenovo laptops only, it makes use of Sensor.dll. I've used code below:
[DllImport("sensor.dll")]
private static extern void ShockproofGetAccelerometerData(ref APSReading accData);I'm not sure how should I check if it exits on the system or not. I'm using lenovo laptop therefore program works fine on my system but If someone runs on different system then it should show a message "system doesn't support" or some other error message. Is it possible to check if certain dll is present in system before importing it?? Thank you Shivam Kalra :)
-
Hello everyone, I'm making a program that works on some lenovo laptops only, it makes use of Sensor.dll. I've used code below:
[DllImport("sensor.dll")]
private static extern void ShockproofGetAccelerometerData(ref APSReading accData);I'm not sure how should I check if it exits on the system or not. I'm using lenovo laptop therefore program works fine on my system but If someone runs on different system then it should show a message "system doesn't support" or some other error message. Is it possible to check if certain dll is present in system before importing it?? Thank you Shivam Kalra :)
Hi, the easiest way is by putting your DLL calls (or at least the first one you are going to do) in a try block. The actual lookup of the DLL is postponed till it is actually called, and any error locating it, or its functions, is clearly turned into an exception. You can find more about it under "Typical Exceptions" in my article here[^]. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hello everyone, I'm making a program that works on some lenovo laptops only, it makes use of Sensor.dll. I've used code below:
[DllImport("sensor.dll")]
private static extern void ShockproofGetAccelerometerData(ref APSReading accData);I'm not sure how should I check if it exits on the system or not. I'm using lenovo laptop therefore program works fine on my system but If someone runs on different system then it should show a message "system doesn't support" or some other error message. Is it possible to check if certain dll is present in system before importing it?? Thank you Shivam Kalra :)
You'll need to dynamically load the DLL at runtime so you can catch if it's not there. Searching on "c# dynamically load unmanaged dll" yields examples, like this one... Dynamically calling an unmanaged dll from .NET (C#)[^]
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You'll need to dynamically load the DLL at runtime so you can catch if it's not there. Searching on "c# dynamically load unmanaged dll" yields examples, like this one... Dynamically calling an unmanaged dll from .NET (C#)[^]
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thank you guys. I also found another way of doing it using Files.Exists method of .net.
-
Thank you guys. I also found another way of doing it using Files.Exists method of .net.
That still lets you use DllImportAttribute??
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
That still lets you use DllImportAttribute??
Mark Salsbery Microsoft MVP - Visual C++ :java:
In my experience DllImport is not interested in the DLL at all (it is up to the programmer to make sure there eventually will be a DLL file and a function that matches with his prototype). A C# app builds just fine without the DLL being present; and the calling code throws a DllNotFoundException at run-time whenever it doesn't find the DLL. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
That still lets you use DllImportAttribute??
Mark Salsbery Microsoft MVP - Visual C++ :java:
Yes off course. I'm just using it as a checking statement..if it doesn't exit then I don't import the dll. :) By the way I just finished the project I was working on. I'm not publicizing but here is the link to my blog post. http://shivam-kalra.blogspot.com/2011/05/using-in-build-accelerometer-in-lenovo.html[^] And thanks for the help! Shivam Kalra :)
-
In my experience DllImport is not interested in the DLL at all (it is up to the programmer to make sure there eventually will be a DLL file and a function that matches with his prototype). A C# app builds just fine without the DLL being present; and the calling code throws a DllNotFoundException at run-time whenever it doesn't find the DLL. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Good to know! Thanks Luc.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Yes off course. I'm just using it as a checking statement..if it doesn't exit then I don't import the dll. :) By the way I just finished the project I was working on. I'm not publicizing but here is the link to my blog post. http://shivam-kalra.blogspot.com/2011/05/using-in-build-accelerometer-in-lenovo.html[^] And thanks for the help! Shivam Kalra :)
Cool thank you!
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Good to know! Thanks Luc.
Mark Salsbery Microsoft MVP - Visual C++ :java:
You're welcome. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.