Solved

Nnotes.dll API Create Empty Replica Stubs through VB.NET 2008

Posted on 2009-05-05
36
2,521 Views
Last Modified: 2013-12-18
Hi guys,

I hope you can help me, I'm looking for a way to create empty replica's of existing notes database on a different server in VB.NET 2008.

After searching the net I've found that the only way to do this is, is through to use of the Notes C API.

Could anyone provide the necessary code to accomplish this in a VB.NET 2008 application?

My own implementation keeps on telling me :

System.AccessVoilation Error
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Method : Int32 NSFDbCreateAndCopy(System.String, System.String, Int32, Int32, Int64, Int32 ByRef)

I think this is because I'm using the COM interface in conjunction with a DLLImport
My guess is that i will need to use the API functions to start session, open database before executing the creation of the replica, but I can't seem to get it right.

Any help would be appreciated!

Thanks

Kraeven
imports domino
 

Module CreateReplica
 

Const NOTE_CLASS_NONE = 0                                                      

Const DBCOPY_REPLICA = 1

Dim hDb as integer
 
 

<DllImport("nnotes.dll")> Public Function _

              NSFDbCreateAndCopy( _

        ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As Integer, ByVal limit As Integer, ByVal flags As Long, ByRef hDb As Integer) As Integer

    End Function
 

Public Function CreateReplicaStubs(ByVal sourceserver As String, ByVal destinationserver As String, ByVal replicaId As String,byval password as string) As Boolean

        Try

           Dim session as Domino.NotesSession

            Dim databaseke As NotesDatabase = Nothing
 

            Dim dbDir As Domino.NotesDbDirectory

            Dim SrcPath As String

            Dim DestPath As String

            Dim result As Integer

            session = New Domino.NotesSession

            session.Initialize(password)

            dbDir = session.GetDbDirectory(sourceserver)

            databaseke = dbDir.OpenDatabaseByReplicaID(replicaId)

            If databaseke.IsOpen Then

               

                SrcPath = sourceserver + "!!" + databaseke.FilePath

                DestPath = destinationserver + "!!" + databaseke.FilePath

                result = NSFDbCreateAndCopy(SrcPath, DestPath, NOTE_CLASS_NONE, 0, DBCOPY_REPLICA, hdb)

                If result = 0 Then

                        Return True

               Else

                         Return False

               End If

            else

                    Return false

            End If

        Catch ex As Exception

           Msgbox("Error occured while creating replica stub on : " + destinationserver + vbCrLf + ex.ToString)

            Return False

        End Try
 

    End Function
 

end module

Open in new window

0
Comment
Question by:Kraeven
  • 17
  • 10
  • 5
  • +1
36 Comments
 
LVL 3

Author Comment

by:Kraeven
ID: 24302965
I've also tried this with the declare function

Declare Function W32_NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As Integer, ByVal limit As Integer, ByVal flags As Long, ByRef hDb As Long) As Integer

No luck!
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24304021
Try with the declaration on this pageke:

http://www.notes411.com/dominosource/tips.nsf/0/A8AFDA9E8C85450D8025706C00459D5F!opendocument

How on earth that can work using Double I fail to see... So this one seems better:

http://www.experts-exchange.com/Q_20174861.html
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 24304235
Here is the LotusScript code that I use in production.  Sorry, I don't have time to port it to VB.NET.
Public Type TIMEDATE

	Innards(1) As Long

End Type
 

Public Type DBREPLICAINFO

	ID As TIMEDATE

	Flags As Integer

	CutoffInterval As Integer

	Cutoff As TIMEDATE

End Type
 

Public Const NOERROR = &H0000

Public Const DBCLASS_NOTEFILE = &HFF01
 

Declare Public Function W32_NSFDbOpen Lib "NNOTES" Alias "NSFDbOpen" (Byval pathName As String, retDbHandle As Long) As Integer

Declare Public Function W32_NSFDbClose Lib "NNOTES" Alias "NSFDbClose" (Byval dbHandle As Long) As Integer

Declare Public Function W32_NSFDbCreate Lib "NNOTES" Alias "NSFDbCreate" (Byval dbname As String, Byval dbClass As Single, Byval forceIt As Single) As Long

Declare Public Function W32_NSFDbReplicaInfoGet Lib "NNOTES" Alias "NSFDbReplicaInfoGet" (Byval hDb As Long, replicainfo As DBREPLICAINFO) As Long

Declare Public Function W32_NSFDbReplicaInfoSet Lib "NNOTES" Alias "NSFDbReplicaInfoSet" (Byval hDb As Long, replicainfo As DBREPLICAINFO) As Long

Declare Public Function W32_OSPathNetConstruct Lib "NNOTES" Alias "OSPathNetConstruct" (Byval portName As Long, Byval serverName As String, Byval fileName As String, Byval retPathName As String) As Integer

Declare Public Sub W32_OSLoadString Lib "NNOTES" Alias "OSLoadString" (Byval null1 As Long, Byval sError As Integer, Byval errstr As String, Byval lenstr As Integer)
 

Public Function CreateLocalReplicaStub(Byval sourceServer As String, Byval sourcePath As String, Byval targetPath As String, Byval overwrite As Integer, Byval setFlags As Long, Byval clearFlags As Long) As Boolean

	'/**

	' * Creates a replica 'stub'.

	' * @param sourceServer The server wherer the database resides.

	' * @param sourcePath The path to the database file.

	' * @param targetPath The path to the local replica.

	' * @param overwrite If True, the local file will be overwritten if it already exists.

	' * @param setFlags Replication flags to set.  Specify 0 for no change.

	' * @param clearFlags Replication flags to clear.  Specify 0 for no change.

	' */

	Dim sess As New NotesSession

	Dim replicaPath As String

	Dim replicaInfo As DBREPLICAINFO

	Dim hSource As Long

	Dim hReplica As Long

	Dim status As Long

	CreateLocalReplicaStub = False

	If (Not IsDefined("WIN32")) Then Exit Function

	' If the replica already exists, bail.

	Dim dbSource As New NotesDatabase("", "")

	Dim dbReplica As New NotesDatabase("", "")

	If (Not dbSource.Open(sourceServer, sourcePath)) Then Exit Function

	If (dbReplica.OpenByReplicaID("", dbSource.ReplicaID)) Then

		Dim dbCurrent As NotesDatabase

		Set dbCurrent = sess.CurrentDatabase

		If (Not dbCurrent Is dbSource) Then

			Call dbSource.Close()

			Set dbSource = Nothing

		End If

		If (Not dbCurrent Is dbReplica) Then

			Call dbReplica.Close()

			Set dbReplica = Nothing

		End If

		CreateLocalReplicaStub = True

		Exit Function

	End If

	' Release the LotusScript handle the source database.

	If (Not dbCurrent Is dbSource) Then

		Call dbSource.Close()

		Set dbSource = Nothing

	End If

	' Get replica info from the source database.

	hSource = NsfDbOpen(sourceServer, sourcePath)

	status = W32_NSFDbReplicaInfoGet(hSource, replicaInfo)

	Call CheckNotesApiStatus(status)

	status = W32_NSFDbClose(hSource)

	Call CheckNotesApiStatus(status)

	' Set the replication info.

	replicaInfo.Flags = replicaInfo.Flags Or setFlags

	replicaInfo.Flags = replicaInfo.Flags Xor clearFlags

	' Create the new replica.

	replicaPath = Space(1024)

	Call W32_OSPathNetConstruct(0, "", targetPath, replicaPath)

	status = W32_NSFDbCreate(replicaPath, DBCLASS_NOTEFILE, overwrite)

	Call CheckNotesApiStatus(status)

	status = W32_NSFDbOpen(replicaPath, hReplica)

	Call CheckNotesApiStatus(status)

	status = W32_NSFDbReplicaInfoSet(hReplica, replicaInfo)

	Call CheckNotesApiStatus(status)

	status = W32_NSFDbClose(hReplica)

	Call CheckNotesApiStatus(status)

	' Add icons to workspace.

	If (Not sess.IsOnServer) Then

		On Error Resume Next

		Dim ws As New NotesUIWorkspace()

		Call ws.AddDatabase(sourceServer, sourcePath)

		Call ws.AddDatabase("", targetPath)

	End If

	CreateLocalReplicaStub = True

End Function
 

Public Function NsfDbOpen(Byval server As String, Byval filePath As String) As Long

	'/**

	' * Returns a Notes API database handle.

	' * @param db The NotesDatabase object for the database to open.

	' * @return The api handle to the specified database.

	' */

	Dim path As String

	Dim hDb As Long

	Dim status As Integer

	path = Space(1024)

	Call W32_OSPathNetConstruct(0, server, filePath, path)

	status = W32_NSFDbOpen(path, hDb)

	Call CheckNotesApiStatus(status)

	If (hDb = 0) Then Error 2000, "Unable to open database (" & filePath & ")."

	NSFDbOpen = hDb

End Function
 

Public Sub CheckNotesApiStatus(Byval status As Long)

	'/**

	' * Checks the result of a Notes Api function for a valid status.  If an error occurred, an error is raised.

	' * @param status The result of a Notes Api function call.

	' */

	Dim ret As String

	status = status And 4095

	If (status = NOERROR) Then Exit Sub

	ret = String(255, 0)

	Call W32_OSLoadString(0, status And &h03FFFFFFF, ret, 255)

	ret = Left(ret, Instr(ret, Chr$(0)) -1)

	Error status, ret	

End Sub

Open in new window

0
 
LVL 3

Author Comment

by:Kraeven
ID: 24312718
Bill-Hanson,


I've tried to convert your code to VB.NET but I can't seem to get it right.
Could anyone provide a simple function to create a replica stub in VB.NET?

Will I have to initiate a session through the API?
Open a database through this API and maybe only then creating the stubs?
How can this be done?

Does anyone have a api wrapper to accomplish this?
What's the best solution?

Kraeven


0
 
LVL 3

Author Comment

by:Kraeven
ID: 24312905
I have been trying to use the API to initiate a Lotus Notes session, open database and creating the stub.
But still no luck
 Declare Function NotesInitExtended Lib "nnotes.dll" (ByVal argc As Long, ByVal argv As String) As Integer

    Declare Function NotesTerm Lib "nnotes.dll" () As Integer

    Declare Auto Sub NotesInitThread Lib "nnotes" ()

    Declare Auto Sub NotesTermThread Lib "nnotes" ()

    Declare Function NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As long, ByVal limit As long, ByVal flags As Long, ByRef hDb As Long) As Integer

    Declare Function NSFDbClose Lib "nnotes.dll" Alias "NSFDbClose" (ByVal hdb As Long) As Integer

    Declare Function NSFDbOpen Lib "nnotes.dll" (ByVal path As String, ByVal dbhandle As Long) As Integer

    'Const NOTE_CLASS_NONE = &H0&

    'Const DBCOPY_REPLICA = &H1&

    Const NOTE_CLASS_DOCUMENT = 1                                                 '/* document note */

    Const NOTE_CLASS_DATA = NOTE_CLASS_DOCUMENT                '/* old name for document note */

    Const NOTE_CLASS_INFO = 2                                                            '/* notefile info (help-about) note */

    Const NOTE_CLASS_FORM = 4                                                          '/* form note */

    Const NOTE_CLASS_VIEW = 8                                                           '/* view note */

    Const NOTE_CLASS_ICON = 16                                                          '/* icon note */

    Const NOTE_CLASS_DESIGN = 32                                                       '/* design note collection */

    Const NOTE_CLASS_ACL = 64                                                              '/* acl note */

    Const NOTE_CLASS_HELP_INDEX = 128                                             '/* Notes product help index note */

    Const NOTE_CLASS_HELP = 256                                                         '/* designer's help note */

    Const NOTE_CLASS_FILTER = 512                                                       '/* filter note */

    Const NOTE_CLASS_FIELD = 1024                                                       '/* field note */

    Const NOTE_CLASS_REPLFORMULA = 2048                                       '/* replication formula */

    Const NOTE_CLASS_PRIVATE = 4096                                                  '/* Private design note, use $PrivateDesign view to locate/classify */

    Const NOTE_CLASS_DEFAULT = 8192                                                 '/* MODIFIER - default version of each */

    Const NOTE_CLASS_NOTIFYDELETION = NOTE_CLASS_DEFAULT '/* see SEARCH_NOTIFYDELETIONS */

    Const NOTE_CLASS_ALL = 32767                                                        '/* all note types */

    Const NOTE_CLASS_ALLNONDATA = 32766                                       '/* all non-data notes */

    Const NOTE_CLASS_NONE = 0                                                       '/* no notes */

    Const DBCOPY_REPLICA = 1

    Const DBCOPY_SUBCLASS_TEMPLATE = 2

    Const DBCOPY_DBINFO2 = 4

    Const DBCOPY_SPECIAL_OBJECTS = 8

    Const DBCOPY_NO_ACL = 16

    Const DBCOPY_NO_FULLTEXT = 32

    Const DBCOPY_ENCRYPT_SIMPLE = 64

    Const DBCOPY_ENCRYPT_MEDIUM = 128

    Const DBCOPY_ENCRYPT_STRONG = 256

    Const DBCOPY_KEEP_NOTE_MODTIME = 512

    Dim hdb As Integer

 
 

    Public Function CreateReplicaStubs(ByVal sourceserver As String, ByVal destinationserver As String, ByVal dbFilePath as String, Optional ByRef err As String  = "") As Boolean

        Try

           

            Dim SrcPath As String

            Dim DestPath As String

            Dim result As Integer

            Dim res As Integer = NotesInitExtended(0, "")

            Dim res1 As Integer

            SrcPath = sourceserver + "!!" + dbFilePath

            DestPath = destinationserver + "!!" + dbFilePath

            NotesInitThread()

                      If (res1 = NSFDbOpen(SrcPath, hdb)) = 0 Then

                result = NSFDbCreateAndCopy(SrcPath, DestPath, NOTE_CLASS_NONE, 0, DBCOPY_REPLICA, hdb)

                If result = 0 Then

                    If hdb <> 0 Then result = NSFDbClose(hdb)

                    Return True

                Else

                    If hdb <> 0 Then result = NSFDbClose(hdb)

                    Return False

                End If

            End If

 

       

 

        Catch ex As Exception

            err = "Error occured while creating replica stub on : " + destinationserver + vbCrLf + ex.Message + vbCrLf + GetErrorInfo(ex)

            Return False

        Finally

            NotesTerm()

            NotesTermThread()

        End Try

 

    End Function

Open in new window

0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24313302
Please elaborate on "no luck"... What statement does your code stop at? Did you try to use a debugger?
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24313416
Well the NotesInitExtended function gives a return result 102105088, if this would have succeeded I think it should return 0

The program continues untill it tries to open the database :

Line :  If (res1 = NSFDbOpen(SrcPath, hdb)) = 0 Then

Error occured while creating replica stub on : LOCAL
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Method : Int32 NSFDbOpen(System.String ByRef, Int64)

0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24313766
http://www-12.lotus.com/ldd/doc/tools/c/4.6.2/Api462re.nsf/61fd4e9848264ad28525620b006ba8bd/e151a8f6c224633d85255f7f00664516?OpenDocument

NotesInitExtended uses argv[0] to locate the Notes executable directory and the resource directory. Then it performs the same initialization that NotesInit() performs.

So your call with
      Dim res As Integer = NotesInitExtended(0, "")
probably won't work, because the Notes executable directory cannot be found. You could try with

      char *argv= "\\lotus\\notes";  // set it to your Notes binaries directory, or to the path to notes.ini
      Dim res As Integer = NotesInitExtended(1, argv)

Make sure that you set the PATH environment variable correctly too.
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24314444
Tried with NotesInit same as extended.
Declare Function NotesInit Lib "nnotes.dll" () As Integer

    Declare Function NotesInitExtended Lib "nnotes.dll" (ByVal argc As Long, ByVal argv As String) As Integer

    Declare Function NotesTerm Lib "nnotes.dll" () As Integer

    Declare Auto Sub NotesInitThread Lib "nnotes" ()

    Declare Auto Sub NotesTermThread Lib "nnotes" ()

    Declare Function NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As Long, ByVal limit As Long, ByVal flags As Long, ByRef hDb As Long) As Integer

    Declare Function NSFDbClose Lib "nnotes.dll" Alias "NSFDbClose" (ByVal hdb As Long) As Integer

    Declare Function NSFDbOpen Lib "nnotes.dll" (ByVal path As String, ByVal dbhandle As Long) As Integer

    'Const NOTE_CLASS_NONE = &H0&

    'Const DBCOPY_REPLICA = &H1&

    Const NOTE_CLASS_DOCUMENT = 1                                                 '/* document note */

    Const NOTE_CLASS_DATA = NOTE_CLASS_DOCUMENT                '/* old name for document note */

    Const NOTE_CLASS_INFO = 2                                                            '/* notefile info (help-about) note */

    Const NOTE_CLASS_FORM = 4                                                          '/* form note */

    Const NOTE_CLASS_VIEW = 8                                                           '/* view note */

    Const NOTE_CLASS_ICON = 16                                                          '/* icon note */

    Const NOTE_CLASS_DESIGN = 32                                                       '/* design note collection */

    Const NOTE_CLASS_ACL = 64                                                              '/* acl note */

    Const NOTE_CLASS_HELP_INDEX = 128                                             '/* Notes product help index note */

    Const NOTE_CLASS_HELP = 256                                                         '/* designer's help note */

    Const NOTE_CLASS_FILTER = 512                                                       '/* filter note */

    Const NOTE_CLASS_FIELD = 1024                                                       '/* field note */

    Const NOTE_CLASS_REPLFORMULA = 2048                                       '/* replication formula */

    Const NOTE_CLASS_PRIVATE = 4096                                                  '/* Private design note, use $PrivateDesign view to locate/classify */

    Const NOTE_CLASS_DEFAULT = 8192                                                 '/* MODIFIER - default version of each */

    Const NOTE_CLASS_NOTIFYDELETION = NOTE_CLASS_DEFAULT '/* see SEARCH_NOTIFYDELETIONS */

    Const NOTE_CLASS_ALL = 32767                                                        '/* all note types */

    Const NOTE_CLASS_ALLNONDATA = 32766                                       '/* all non-data notes */

    Const NOTE_CLASS_NONE = 0                                                       '/* no notes */

    Const DBCOPY_REPLICA = 1

    Const DBCOPY_SUBCLASS_TEMPLATE = 2

    Const DBCOPY_DBINFO2 = 4

    Const DBCOPY_SPECIAL_OBJECTS = 8

    Const DBCOPY_NO_ACL = 16

    Const DBCOPY_NO_FULLTEXT = 32

    Const DBCOPY_ENCRYPT_SIMPLE = 64

    Const DBCOPY_ENCRYPT_MEDIUM = 128

    Const DBCOPY_ENCRYPT_STRONG = 256

    Const DBCOPY_KEEP_NOTE_MODTIME = 512

    Dim hdb As Integer
 
 

    Public Function CreateReplicaStubs(ByVal sourceserver As String, ByVal destinationserver As String, ByVal replicaId As String, Optional ByRef err As String = "", Optional ByVal dbFilePath As String = "") As Boolean

        Try

            'Dim databaseke As NotesDatabase = Nothing

            Dim SrcPath As String

            Dim DestPath As String

            Dim result As Integer

            Dim res As Integer '= NotesInitExtended(0, "")

            Dim res1 As Integer

            SrcPath = sourceserver + "!!" + dbFilePath

            DestPath = destinationserver + "!!" + dbFilePath

            NotesInitThread()

            res = NotesInit

            'Dim ses As NotesSession

            'Dim notesDir As NotesDbDirectory

            'ses = New NotesSession

            'ses.Initialize(SelectedPassword)
 

            'notesDir = session.GetDbDirectory(sourceserver)
 

            'databaseke = notesDir.OpenDatabaseByReplicaID(replicaId)
 

            'If databaseke.IsOpen Then

            If (res1 = NSFDbOpen(SrcPath, hdb)) = 0 Then

                result = NSFDbCreateAndCopy(SrcPath, DestPath, NOTE_CLASS_NONE, 0, DBCOPY_REPLICA, hdb)

                If result = 0 Then

                    If hdb <> 0 Then result = NSFDbClose(hdb)

                    Return True

                Else

                    If hdb <> 0 Then result = NSFDbClose(hdb)

                    Return False

                End If

            End If
 

       
 

            'End If

        Catch ex As Exception

            err = "Error occured while creating replica stub on : " + destinationserver + vbCrLf + ex.Message + vbCrLf + GetErrorInfo(ex)

            Return False

        Finally

            NotesTerm()

            NotesTermThread()

        End Try
 

    End Function

Open in new window

0
 
LVL 3

Author Comment

by:Kraeven
ID: 24314647
Dim args As String = "C:\program files\Lotus\Notes"
res = NotesInitExtended(1, args)

Is this correctly formatted?
This didn't do it for me.


What do you mean with  char *argv= "\\lotus\\notes";  ====>  Dim argv() As Char = {"C:\program files\Lotus\Notes"} ?

Tried it like this...

Declare Function NotesInitExtended Lib "nnotes.dll" (ByVal argc As Long, ByVal argv() As Char) As Integer

  Dim argv() As Char = {"C:\program files\Lotus\Notes"}
  res = NotesInitExtended(1, args)

Again same error as on previous post.

Error occured while creating replica stub on : LOCAL
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Method : Int32 NSFDbOpen(System.String ByRef, Int64)


0
 
LVL 3

Author Comment

by:Kraeven
ID: 24372430
This is getting to be rather urgent, could anyone provide me with a tested and working function/module/class for VB.NET 2008?

Thanks in advance


Kraeven
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24481631
anyone?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24481815
> What do you mean with  char *argv= "\\lotus\\notes";  

I don't speak VB.NET, so I can't really tell you what the equivalent of the above line is in VB.NET.

This might help you:
http://www.experts-exchange.com/Programming/Languages/C/Q_20577964.html

0
 
LVL 3

Author Comment

by:Kraeven
ID: 24744643
I still haven't found a solution for this problem.

I prefer to write the necessary code in my project but if this isn't possible an executable with commandline parameters could also suffice.

Anyone any suggestions?
0
 

Expert Comment

by:dgallimore
ID: 24786947
You need to modify line

Declare Function NSFDbOpen Lib "nnotes.dll" (ByVal path As String, ByVal dbhandle As Long) As Integer

to

Declare Function NSFDbOpen Lib "nnotes.dll" (ByVal path As String, ByRef dbhandle As Long) As Integer

in order for the Open API to return the dbHandle.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24787141
It hard to imagine that that would help. IMHO, even this code would be accepted:

Declare Function NSFDbOpen Lib "nnotes.dll" Alias "NSFDbOpen" ( Byval dbName As String, hDb As Long) As Integer

The names of the parameters are irrelevant, but it might just be the missing alias, being case-sensitive...
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24787158
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Expert Comment

by:dgallimore
ID: 24792010
The dbHandle is returned by the call NSFDbOpen. Hence passing it by value passes the current value of the parameter rather than a pointer to it's location in memory.
I've struggled for some time with getting the C API toolkit working with .NET and the accuracy of the parameter types is very important.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24792039
Sooooooooooo sorry.... You're absolutely right!

*sneaking out the back door...*
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24792043
dgallimore,

Could you provide me with a sample source code of your implementation?
From the beginning of the initialization until the closing of the session?
I want to use this function in a VB.NET program, for creating new databases derrived from a generic database. No matter what I try, I can't get it working, so my guess is that I'm doing it all wrong from the start.

Many thanks out front.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 24792071
The first thing you'd have to do is adopt the corrected declare statement for the Open function, the 2nd ByVal in the declaration must be removed. That should already make a huge difference, so try again.

If you did try again, what doesn't work? How far does your application get in your code?
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24792151
Sjef,

The function seems to be able to open the database (hDb=7), but following error occures :

Error occured while creating replica stub on :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Method : Int32 NSFDbCreateAndCopy(System.String ByRef, System.String ByRef, Int64, Int64, Int64, Int64 ByRef)
LineN° : 0
Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\AcK\NotesAgents\NotesAgents\Agents.vb
Method : Boolean CreateReplicaStubs(System.String, System.String, System.String, System.String ByRef, System.String)
LineN° : 2449

 Declare Function NotesInit Lib "nnotes.dll" () As Integer

 Declare Function NotesTerm Lib "nnotes.dll" () As Integer

 Declare Function NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As Long, ByVal limit As Long, ByVal flags As Long, ByRef hDb As Long) As Integer

 Declare Function NSFDbClose Lib "nnotes.dll" Alias "NSFDbClose" (ByVal hDb As Long) As Integer

 Declare Function NSFDbOpen Lib "nnotes.dll" (ByVal path As String, ByRef hDB As Long) As Integer

    'Const NOTE_CLASS_NONE = &H0&

    'Const DBCOPY_REPLICA = &H1&

    Const NOTE_CLASS_DOCUMENT = 1                                                 '/* document note */

    Const NOTE_CLASS_DATA = NOTE_CLASS_DOCUMENT                '/* old name for document note */

    Const NOTE_CLASS_INFO = 2                                                            '/* notefile info (help-about) note */

    Const NOTE_CLASS_FORM = 4                                                          '/* form note */

    Const NOTE_CLASS_VIEW = 8                                                           '/* view note */

    Const NOTE_CLASS_ICON = 16                                                          '/* icon note */

    Const NOTE_CLASS_DESIGN = 32                                                       '/* design note collection */

    Const NOTE_CLASS_ACL = 64                                                              '/* acl note */

    Const NOTE_CLASS_HELP_INDEX = 128                                             '/* Notes product help index note */

    Const NOTE_CLASS_HELP = 256                                                         '/* designer's help note */

    Const NOTE_CLASS_FILTER = 512                                                       '/* filter note */

    Const NOTE_CLASS_FIELD = 1024                                                       '/* field note */

    Const NOTE_CLASS_REPLFORMULA = 2048                                       '/* replication formula */

    Const NOTE_CLASS_PRIVATE = 4096                                                  '/* Private design note, use $PrivateDesign view to locate/classify */

    Const NOTE_CLASS_DEFAULT = 8192                                                 '/* MODIFIER - default version of each */

    Const NOTE_CLASS_NOTIFYDELETION = NOTE_CLASS_DEFAULT '/* see SEARCH_NOTIFYDELETIONS */

    Const NOTE_CLASS_ALL = 32767                                                        '/* all note types */

    Const NOTE_CLASS_ALLNONDATA = 32766                                       '/* all non-data notes */

    Const NOTE_CLASS_NONE = 0                                                       '/* no notes */

    Const DBCOPY_REPLICA = 1

    Const DBCOPY_SUBCLASS_TEMPLATE = 2

    Const DBCOPY_DBINFO2 = 4

    Const DBCOPY_SPECIAL_OBJECTS = 8

    Const DBCOPY_NO_ACL = 16

    Const DBCOPY_NO_FULLTEXT = 32

    Const DBCOPY_ENCRYPT_SIMPLE = 64

    Const DBCOPY_ENCRYPT_MEDIUM = 128

    Const DBCOPY_ENCRYPT_STRONG = 256

    Const DBCOPY_KEEP_NOTE_MODTIME = 512
 

Public Function CreateReplicaStubs(ByVal sourceserver As String, ByVal destinationserver As String, ByVal replicaId As String, Optional ByRef err As String = "", Optional ByVal dbFilePath As String = "") As Boolean

        Try

            

            Dim SrcPath As String

            Dim DestPath As String

            Dim result As Integer

            Dim hDb As Integer

            Dim res As Integer 

            

            SrcPath = sourceserver + "!!" + dbFilePath

            DestPath = destinationserver + "!!" + dbFilePath

           

            res = NotesInit

           

            If (res = NSFDbOpen(SrcPath, hDb)) = 0 Then

                result = NSFDbCreateAndCopy(SrcPath, DestPath, NOTE_CLASS_NONE, 0, DBCOPY_REPLICA, hDb)

                If result = 0 Then

                    If hDb <> 0 Then result = NSFDbClose(hDb)

                    Return True

                Else

                    If hDb <> 0 Then result = NSFDbClose(hDb)

                    Return False

                End If

            End If

                 

        Catch ex As Exception

            err = "Error occured while creating replica stub on : " + destinationserver + vbCrLf + ex.Message + GetErrorInfo(ex)

            Return False

        Finally

            NotesTerm()

        End Try
 

    End Function

Open in new window

0
 

Expert Comment

by:dgallimore
ID: 24792241
I've not used the NSFDbCreateAndCopy function.
The declaration in the C API Toolkit is

STATUS LNPUBLIC NSFDbCreateAndCopy(const char far* srcDb,const char far* dstDb, WORD NoteClass, WORD limit, DWORD flags, DBHANDLE far* retHandle);

Your declaration:

Declare Function NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As Long, ByVal limit As Long, ByVal flags As Long, ByRef hDb As Long) As Integer

doesn't exactly match this as the limit parameter is a WORD (i.e. UInt16) rather than a Long.

You need to check each and every parameter or the Notes stack gets corrupted and then Notes dies.
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24792373
Declare Function NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As UInt16, ByVal limit As UInt16, ByVal flags As Long, ByRef hDb As Long) As Integer


Remains the same error

Error occured while creating replica stub on :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Method : Int32 NSFDbCreateAndCopy(System.String ByRef, System.String ByRef, UInt16, UInt16, Int64, Int64 ByRef)
LineN° : 0
Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\AcK\NotesAgents\NotesAgents\Agents.vb
Method : Boolean CreateReplicaStubs(System.String, System.String, System.String, System.String ByRef, System.String)
LineN° : 2449
0
 

Expert Comment

by:dgallimore
ID: 24792661
The return value is a UInt16.
This may seem picky but the stack is corrupted if the wrong number of bytes is put on/taken off during a function call.
Other possibilities with this function call is that specific marshalling may be required for the String parameters.
It may be for this function you need to marshall the Strings by using System.Runtime.InteropServices  MarshalAs(UnmanagedType.LPStr).
I've shown LPStr is an example, but it may be ANSIBStr or one of the other types. You will need to check exactly how the strings are sent to this function. Sorry I can't be more specific but this may point you in the right direction.
0
 
LVL 3

Author Comment

by:Kraeven
ID: 24792755
Do you mean this :

Declare Function NSFDbCreateAndCopy Lib "nnotes.dll" Alias "NSFDbCreateAndCopy" (ByVal srcDb As String, ByVal dstDb As String, ByVal noteClass As UInt16, ByVal limit As UInt16, ByVal flags As Long, ByRef hDb As Long) As UInt16

Error occured while creating replica stub on :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Method : UInt16 NSFDbCreateAndCopy(System.String ByRef, System.String ByRef, UInt16, UInt16, Int64, Int64 ByRef)
LineN° : 0
Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\AcK\NotesAgents\NotesAgents\Agents.vb
Method : Boolean CreateReplicaStubs(System.String, System.String, System.String, System.String ByRef, System.String)
LineN° : 2449

Same error...

Can you provide a sample of the marshalling of the strings? Never done that...
Do you have a sample code of a function using the c api, just for reference and to get some ideas about how to handle the api in general.

Thanks for the quick responses guys ;-)
0
 

Expert Comment

by:dgallimore
ID: 24792870
This opens up a new barrel of worms.
I've used a class to wrap the functions in C#. Beginning of class attached.
In C#, to set the marshalling, you add a prefix to the variable.
e.g. [MarshalAs(UnmanagedType.LPStr)]String pathName
Hope this helps.


using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

using NotesTypes;
 

namespace Test2

{

    using HANDLE = System.UInt32;

    using WORD = System.UInt16;

    using DWORD = System.UInt32;

    using STATUS = System.UInt16;

    using NOTEID = System.UInt64;
 

    class NsfApi

    {

       

        [DllImport("nnotes.dll")] 

        public static extern void NotesInit();
 

        [DllImport("nnotes.dll")]

        public static extern void NotesTerm();
 

        /*

         * STATUS LNPUBLIC NSFDbOpen( 

         *  const char far *PathName, 

         *  DBHANDLE far *rethDB); 

         */

        [DllImport("nnotes.dll")]

        public static extern STATUS NSFDbOpen(String pathName, ref HANDLE phDb);

Open in new window

0
 
LVL 3

Author Comment

by:Kraeven
ID: 24906283
Tried this but still can't get it working, anyone able to provide me with a sample code to accomplish this?
A dll I can reffer to would also be good...

Please provide a fully working code if possible...

Many thanks!
0
 
LVL 3

Author Comment

by:Kraeven
ID: 25130950
Still no answer guys?
Could some one provide me with a working sample code in VB.NET?
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 25133208
This is probably not the solution you are looking for, but If I was having this much trouble, I would simply write a small stand-alone helper EXE or DLL using C, where I know there are lots of examples and tested code.   Then, just call the helper app from your main app.

Just my 2 cents.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 25133406
I second this motion (as for the DLL).
0
 
LVL 3

Author Comment

by:Kraeven
ID: 25139772
Could you provide me with a C DLL that I can reference in VB.NET?
Where could I find this and how can I incorporate this in a VB.NET form application?
Most likely it will need a com wrapper or something? This is something I've never tried.
If possible please provide me such a DLL with a working code in VB.NET and some explanation.
0
 
LVL 3

Author Comment

by:Kraeven
ID: 25139779
Still hoping to find a solution for this..
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 500 total points
ID: 25140660
You'll find examples of DLLs in the C-API Toolkit, downloadable from IBM:
https://www.ibm.com/developerworks/lotus/documentation/capi/
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now