dcsimg
Results 1 to 13 of 13
  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    576

    Detect DLL support

    I found an API call that simplifies the Hash process by combining several steps into one:

    http://www.vbforums.com/showthread.p...017-VB6-Hash10

    It only works with Win 10, and I was curious if there was a performance advantage. So I hashed a 7,042 KB file several times with the old process, and with the newer process on the same Win 10 machine. The average time taken with the old process was 0.101 seconds, compared to 0.065 seconds with the newer process. I cannot explain why, but there does seem to be an advantage to using the newer process when it is available. That would mean being able to detect if the newer call was supported. So I came up with this:
    Code:
        On Error Resume Next
        'Test if Win 10 API supported using dummy call
        Call BCryptHash(0&, 0&, 0&, 0&, 0&, 0&, 0&)
        If Err = 453 Then 'Use original API cslls
            bHash = HashData(StrPtr(HashAlg), bBuffer)
        Else 'Use Win 10 Calls
            bHash = Hash10Data(StrPtr(HashAlg), bBuffer)
        End If
    Is there a better way to accomplish this?

    J.A. Coutts

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,353

    Re: Detect DLL support

    Another possibility is to load the DLL and test for the function, using APIs: LoadLibrary, GetProcAddress, FreeLibrary. Not sure this has any real advantage over a simple On Error statement you are using, but it feels cleaner. In any case, your solution could be used at project startup and a boolean cached to indicate whether this option is available or not. That way you don't have to perform your solution every time you want to use that API.

    Code:
    Dim hMod As Long, pa As Long
    
    hMod = LoadLibrary("Bcrypt.dll") 
    If hMod Then
        pa = GetProcAddress(hMod, "BCryptHash") 
        FreeLibrary hMod
    End If
    
    ' if pa<>0 then library exists and so does expected function
    Can someone copy the DLL to an unsupported O/S and the DLL will load properly (hMod returns non-zero) via LoadLibary call? If so, then I am assuming that the BCryptHash will fail. If you have access to an older O/S, you can test this yourself. Typically, a DLL will fail to load if it has dependencies that can't be loaded (don't exist on the O/S) or the DLL is hardcoded for specific minimum O/S. There may be exceptions, but I don't know of any that successfully load, but cannot be executed.

    Edited and a note: I prefer this method vs checking for a specific O/S for one reason. Many DLLs offered in newer O/S will work on an older O/S that didn't distribute that DLL. For example, GDI+ v1.0 was introduced around XP. But it could be redistributed to lower O/S (all the way back to Win95) and worked. In this case, just testing the O/S isn't a good enough test. I realize testing the O/S was not a solution you promoted, but it is a common solution and not a good one for various reasons.
    Last edited by LaVolpe; Jul 2nd, 2018 at 10:06 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    576

    Re: Detect DLL support

    Quote Originally Posted by LaVolpe View Post
    Can someone copy the DLL to an unsupported O/S and the DLL will load properly (hMod returns non-zero) via LoadLibary call? If so, then I am assuming that the BCryptHash will fail. If you have access to an older O/S, you can test this yourself. Typically, a DLL will fail to load if it has dependencies that can't be loaded (don't exist on the O/S) or the DLL is hardcoded for specific minimum O/S. There may be exceptions, but I don't know of any that successfully load, but cannot be executed.
    Copied the Win 10 DLL to the VB6 directory as bcrypt10.dll and tested. The library loaded, but pa returned zero. I do however like your method better than the one I came up with.

    J.A. Coutts

  4. #4
    Fanatic Member
    Join Date
    Apr 2012
    Posts
    1,007

    Re: Detect DLL support

    Quote Originally Posted by LaVolpe View Post
    I realize testing the O/S was not a solution you promoted, but it is a common solution and not a good one for various reasons.
    I understand the point you are making, but if a library is targeted at a minimum OS version, then isn't it a good idea to respect that? After all, something that might work today on an older (unsupported OS), may break in a later library release. This approach seems much more valid if you are not the author of said library (i.e. you are distributing some 3rd party dll).
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,353

    Re: Detect DLL support

    Copied the Win 10 DLL to the VB6 directory as bcrypt10.dll and tested. The library loaded, but pa returned zero.
    pa returning zero is unexpected if hMod returned non-zero and the DLL does in fact export the BCryptHash function. That may be correct. Just ensure you used the correct API versions, Ansi vs Wide and/or pass the strings correctly with/without StrPtr().
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,353

    Re: Detect DLL support

    Quote Originally Posted by ColinE66 View Post
    I understand the point you are making, but if a library is targeted at a minimum OS version, then isn't it a good idea to respect that?
    Tough call. Do you see any MSDN articles that say the minimum supported O/S is Win95? Even they don't provide that info any longer, they just jump right to Vista/Win7 nowadays. In any case, if the O/S was designed for a specific O/S, it likely won't load via LoadLibarary because most DLLs have dependencies and one or more of those won't load because they don't exist on the target O/S. I hear what you are saying, but I haven't had any issues using the method I suggested.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    576

    Re: Detect DLL support

    Quote Originally Posted by LaVolpe View Post
    pa returning zero is unexpected if hMod returned non-zero and the DLL does in fact export the BCryptHash function. That may be correct. Just ensure you used the correct API versions, Ansi vs Wide and/or pass the strings correctly with/without StrPtr().
    API calls were verified by looking for BCryptCreateHash on Win 8.1. There could be many reasons that the new library/call does not work on Win 8.1, not the least of which could be Microsoft's desire to restrict new features to Win 10 only. This does seem to be the case more than any previous operating system.

    J.A. Coutts

    As an interesting side note, the new 32 bit BCrypt.dll is smaller than the old one.
    Last edited by couttsj; Jul 2nd, 2018 at 11:23 AM.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,353

    Re: Detect DLL support

    @Coutts. That DLL does have several dependencies, one of them is BCRYPTPRIMITIVES.DLL (DelayLoad). If that doesn't exist on the target machine, then maybe it explains the failure? That dependency may well exist on Win8.1 too and if so, is not the reason. I am not aware of GetProcAddress being O/S aware regarding a loaded DLL.

    Regarding the method I suggested, it is similar to one that Microsoft suggests...
    If the function might not exist in the DLL module—for example, if the function is available only on Windows Vista but the application might be running on Windows XP—specify the function by name rather than by ordinal value and design your application to handle the case when the function is not available
    Last edited by LaVolpe; Jul 2nd, 2018 at 11:30 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    576

    Re: Detect DLL support

    Hold onto your horses a minute. I renamed the imported DLL back to it's original name to see if that made a difference, and it didn't. In the meantime I had to do some chores, and the system went to sleep. When I woke it up, BCryptHash worked properly on Win 8.1. I assume that the original DLL was still cached in the HEAP, and the system going to sleep cleared the cache. There could still be some issues with other BCrypt functions, so it will take further testing.

    J.A. Coutts

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,353

    Re: Detect DLL support

    In that case, you may find that the new DLL's functions work since it appears it is simply a wrapper for existing functions that existed before Win10. That is the big test. Since the function can be found in the DLL, it can be called. But will it work? If it fails to work, then your solution of using On Error seems to be the best option.

    Above being said, don't know if it would be commonplace to manually replace an existing DLL (same name) from a newer O/S onto an older O/S. A typical user probably isn't going to make that move, though a custom installer might?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    576

    Re: Detect DLL support

    When I said the BCryptHash function worked on Win 8.1, I meant that it actually hashed a string. My understanding is that Windows will first check the default directory, then the Windows directory, and then the System32/Syswow64 directory. By placing the bcrypt.dll in the VB6 directory, Windows chose the new file from that directory by default. Whether it is safe to replace/upgrade the original Win 8.1 will take some testing, but everything I have tried so far works.

    Old Version
    Version 6.3.9600.18541
    Size 108 KB
    Date modified 11/19/2016

    New Version
    Version 10.0.17134.112
    Size 94.8 KB
    Date modified 6/8/2018

    J.A. Coutts

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,922

    Re: Detect DLL support

    Just don't do this.

    The main reason is that if a flaw is discovered and patched your private copy will never get the fix. But there are also legal issues with grabbing OS components and plopping them on another system. And if anything gets changed along the path of dependencies the wheels may come off anyway.

    It just isn't all that hard to make the series of older calls yourself when you need downlevel support. Just write your own procedure to do what this new call does.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    576

    Re: Detect DLL support

    Quote Originally Posted by dilettante View Post
    Just don't do this.

    The main reason is that if a flaw is discovered and patched your private copy will never get the fix. But there are also legal issues with grabbing OS components and plopping them on another system. And if anything gets changed along the path of dependencies the wheels may come off anyway.

    It just isn't all that hard to make the series of older calls yourself when you need downlevel support. Just write your own procedure to do what this new call does.
    Normally I would agree with you, but the performance difference on Win 10 was so large that I thought it was worth investigating. In the end, the performance difference on the Win 8.1 machine using the upgraded DLL was minimal, and not worth the effort. There must be other factors at play. Detecting if it is supported on Win 10 however, does seem well worth the effort.

    J.A. Coutts

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width