Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DllImport issue in c sharp

DllImport issue in c sharp

Scheduled Pinned Locked Moved C#
helpcsharpiotquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shivamkalra
    wrote on last edited by
    #1

    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 :)

    L M 2 Replies Last reply
    0
    • S shivamkalra

      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 :)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S shivamkalra

        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 :)

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        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:

        S 1 Reply Last reply
        0
        • M Mark Salsbery

          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:

          S Offline
          S Offline
          shivamkalra
          wrote on last edited by
          #4

          Thank you guys. I also found another way of doing it using Files.Exists method of .net.

          M 1 Reply Last reply
          0
          • S shivamkalra

            Thank you guys. I also found another way of doing it using Files.Exists method of .net.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            That still lets you use DllImportAttribute??

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            L S 2 Replies Last reply
            0
            • M Mark Salsbery

              That still lets you use DllImportAttribute??

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • M Mark Salsbery

                That still lets you use DllImportAttribute??

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                S Offline
                S Offline
                shivamkalra
                wrote on last edited by
                #7

                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 :)

                M 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Good to know! Thanks Luc.

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  L 1 Reply Last reply
                  0
                  • S shivamkalra

                    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 :)

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    Cool thank you!

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • M Mark Salsbery

                      Good to know! Thanks Luc.

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups