0

I'm using the SSDeep fuzzy.dll to perform fuzzy hashing on a large number of files.

If I run the hashes sequentially, it all works fine. If I try to use multiple threads, it falls over (The app terminates with no exception information and nothing in the logs)

I'm assuming that the DLL is not thread-safe and one thread is attempting to read another's memory or something similar.

What I'd like to do is allow each thread to have its own "copy" of the dll. Note that this isn't an instance as such - It's all static/shared - I simply want to emulate what would happen if 2 processes which referenced the dll were run at the same time - they'd have their own memory space, etc...

Is this possible without actually spawning multiple processes?

<DllImport("C:\SSDeep\Fuzzy.dll",
    EntryPoint:="fuzzy_hash_filename",
    CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function fuzzy_hash_filename(
                <InAttribute(),
                MarshalAsAttribute(UnmanagedType.LPStr)>
                ByVal Filename As String,
                ByVal Result As StringBuilder) As Integer
End Function


Public Shared Function FuzzyHash(Filename As String) As String
    Dim Ret As New StringBuilder
    Ret.Capacity = NativeConstants.FUZZY_MAX_RESULT
    Dim Success = fuzzy_hash_filename(Filename, Ret)
    If Success <> 0 Then
        Throw New Exception("SSDeep fuzzy hashing failed")
    End If
    Return Ret.ToString
End Function
Basic
  • 26,321
  • 24
  • 115
  • 201

2 Answers2

2

To do this you would need either two seperate processes or two different copies of the dll, eg 'fuzzy1.dll' and 'fuzzy2.dll'

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • Nothing clever I could do with AppDomains or similar? Oddly, multiple copies of the dll might actually be preferable to multiple processes although I'd need to generate the interop on-the-fly. I hadn't considered that – Basic Aug 07 '12 at 12:23
1

You can't load a same native DLL multiple times in your different threads. See Load Dll multiple times to allow multi threading in .Net

I see different possible options:

  • spawn multiple processes
  • rename your DLLs as Eamonn McEvoy said, but this requires you to know the number of threads you have and to manually load the DLLs (so it'll be hard to use the ThreadPool for example)
  • enqueue tasks and process them sequentially
Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176