9

I am trying to use pHash from .NET

First thing I tried was to register (regsvr32) phash.dll and asked here Second of all, i was trying to import using DllImport as shown below.

    [DllImport(@".\Com\pHash.dll")]
    public static extern int ph_dct_imagehash(
        [MarshalAs(UnmanagedType.LPStr)] string file, 
        UInt64 hash);

But when i try to access the method above during run-time, following error message shows up.

    Unable to find an entry point named 'ph_dct_imagehash' in DLL '.\Com\pHash.dll'.

What does "entry point" means and why am I getting the error?

Thank you.

FYI - Here is the full source code

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;

namespace DetectSimilarImages
{
    public partial class MainWindow : Window
    {
        [DllImport(@".\Com\pHash.dll")]
        public static extern int ph_dct_imagehash(
            [MarshalAs(UnmanagedType.LPStr)] string file, 
            UInt64 hash);


        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                UInt64 hash1 = 0, hash2 = 0;
                string firstImage = @"C:\Users\dance2die\Pictures\2011-01-23\177.JPG";
                string secondImage = @"C:\Users\dance2die\Pictures\2011-01-23\176.JPG";
                ph_dct_imagehash(firstImage, hash1);
                ph_dct_imagehash(secondImage, hash2);

                Debug.WriteLine(hash1);
                Debug.WriteLine(hash2);
            }
            catch (Exception ex)
            {

            }
        }


    }
}
Community
  • 1
  • 1
dance2die
  • 35,807
  • 39
  • 131
  • 194
  • Did you compile the DLL yourself? If so, did you take care to properly export the relevant function? – Konrad Rudolph Jun 06 '11 at 15:47
  • @konrad: I compiled the source myself but i am lost of what you said on "properly export the relevan functions" because I am not familiar with C++ at all... – dance2die Jun 06 '11 at 16:10
  • 2
    @Sung Then the fix is easy: *don’t* compile the library yourself. Use the precompiled binary. – Konrad Rudolph Jun 06 '11 at 16:12
  • Unfortunately, I still was not able to resolve `Unable to find an entry point named 'ph_dct_imagehash' in DLL '.\Com\pHash.dll'.` eror even with pre-compiled dlls – dance2die Jun 06 '11 at 16:37
  • Note that the second argument to `ph_dct_imagehash` should be `out UInt64 hash` rather than `UInt64 hash`. – ildjarn Jun 06 '11 at 17:14
  • @ildjarn : thank you for the correction. But even though I have tried with new argument, I am still getting the error. So for now I have contacted PHash developer and see what I doing wrong. – dance2die Jun 06 '11 at 18:55
  • @Sung : I knew it wouldn't fix your present, hence posting a comment rather than an answer; just thought it was worth mentioning. ;-] – ildjarn Jun 06 '11 at 19:04
  • 1
    @ildjam:Thank you for mentioned it. i also wasn't sure so I contacted the pHash developer and he told me that DllImport statement should be like `[DllImport("pHash", CharSet=CharSet.Ansi)] public static extern int ph_dct_imagehash(string file, ref ulong hash);` – dance2die Jun 06 '11 at 19:11
  • FYI: https://github.com/ludoviclefevre/phash-integration-in-csharp – Andry Jun 28 '16 at 11:44

1 Answers1

13

The current Windows source code project (as of 7/2011) on phash.org does not seem to export the ph_ API calls from the DLL. You will need to add these yourself by __declspec(dllexport) at the beginning of the line in pHash.h like so:

__declspec(dllexport) int ph_dct_imagehash(const char* file,ulong64 &hash);

You should then see the export show up in the DLL using dumpbin

dumpbin /EXPORTS pHash.dll
...
Dump of file pHash.dll
...
          1    0 00047A14 closedir = @ILT+2575(_closedir)
          2    1 00047398 opendir = @ILT+915(_opendir)
          3    2 00047A4B ph_dct_imagehash = @ILT+2630(_ph_dct_imagehash)
          4    3 000477B2 readdir = @ILT+1965(_readdir)
          5    4 00047A00 rewinddir = @ILT+2555(_rewinddir)
          6    5 000477AD seekdir = @ILT+1960(_seekdir)
          7    6 00047AFA telldir = @ILT+2805(_telldir)

You should now be able to use this call from C#.

however...

I am getting a crash in the CImg code when I try to call this, so it seems there is some more work to do here...

Community
  • 1
  • 1
rupello
  • 8,361
  • 2
  • 37
  • 34
  • This crashes for me in png_read_info(), but it seems to be working OK for JPGs – rupello Jun 07 '11 at 16:31
  • 2
    PNG seems to require some extra steps: http://stackoverflow.com/questions/4001816/loading-pngs-with-cimg – rupello Jun 07 '11 at 16:37
  • I was able to see ' 3 2 00047A4B ph_dct_imagehash = @ILT+2630(_ph_dct_imagehash) ' after doing dumpbin but I still cannot dllimport phash.dll. – dance2die Jun 08 '11 at 19:35
  • What happened here is that, I didn't all dependant dlls and other libraries. After fixing that issue, Now I am getting another error – dance2die Jun 10 '11 at 16:01