Fredrik Norling
Fredrik Norling

Reputation: 3484

Open a C API function within an DLL file from c# gives memory access error

The C API declaration looks like this

include <nsfdb.h>
STATUS LNPUBLIC NSFDbOpen(
    const char far *PathName,
    DBHANDLE far *rethDB
);

https://opensource.hcltechsw.com/domino-c-api-docs/reference/Func/NSFDbOpen/

[DllImport("nnotes.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int NSFDbOpen(string path, ref IntPtr pHandleDb);

I have tried Cdecl and stdcall. I have tried ref / out IntPtr and int

When I do call the function

 int result = NSFDbOpen(dbPath, ref dbHandle);

I get this error

enter image description here

I can add that I've used this in Visual basic 6 and then the handle was a long

=====Update===== in older code it looked like this

Declare Private Function WinNSFDbOpen Lib "nnotes" Alias "NSFDbOpen" ( Byval dbname As String, dbhandle As Long ) As Integer

And the function doing the call like this

Function apiNSFDbOpen ( Byval dbname As String, dbhandle As Long ) As Integer       
    apiNSFDbOpen = winNSFDbOpen ( dbname, dbhandle ) 
End Function

Upvotes: 0

Views: 48

Answers (1)

Charlieface
Charlieface

Reputation: 72128

char* is single-byte, not Unicode.

[DllImport("nnotes.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int NSFDbOpen(string path, ref IntPtr pHandleDb);

Upvotes: 1

Related Questions