P4COM - COM Interface to Perforce C++ API on Windows

p4com is contributed by Robert Cowham of Vaccaperna Systems Ltd. Robert is a Perforce Consulting Partner and Certified Trainer (contact me via that page).

It is a COM component that can be used on Windows by a wide variety of languages to interface to Perforce in a nicer way than spawning p4.exe and parsing the output.

P4COM  vs. P4API.Net/P4.Net

P4API.Net is entirely managed code and is fully supported by Perforce. This is now the recommended interface to use on Windows. There is no need to register any dlls. You can literally use xcopy deployment (or p4 sync :-). P4.Net uses inheritance, indexers and collection interfaces that all give it a more .Net-like interface than P4COM. Having said that, P4COM is still useful in situations where a COM interface is useful.

There is also an earlier (private by Shawn Hladky) version: P4.Net - but this is not fully supported - see also P4.Net

P4COM

I presented a paper on an earlier version at the Perforce User Conference in 2001: Adventures in API Land for an overview of the API and how I use it. In particular pay attention to the section on error handling!

This DLL is part of P4OFC - the integration between Microsoft Office and Perforce and well tested and stable in that environment!

Recent Changes

In November 2014 P4COM was rebuilt using Visual Studio 2010 and the 2014.1 version of the P4API. It also uses the OpenSSL library which means it will now successfully talk to SSL enabled Perforce servers. Because it uses a recent P4API it will handled files in streams depots.

Example Projects

These are to be found in the zip file and in the test subdirectory in the Public Depot.

They demonstrate how to read and set environment values (p4.port, p4.client), and also how to perform a submit using a combination of "p4 change -o", manipulate the results and then execute the command with "p4 submit -i"

There are several example projects (very similar functionality):

There is also:

Just load them up, compile and run and you should be fine (provided DLL was previously registered).

Note that Nick Levine has now made available a Lisp interface (with LispWorks) package which uses P4COM - see:

Installation/Download

You can download the complete source and a pre-built (release) version of p4com.dll from p4com.zip or from the src directory in the Perforce Public Repository (browse via ftp).

You will also need to download the following two (pre-built) files: libeay32.dll and ssleay32.dll - install these as below before trying to register P4COM

To use it, you need to register the DLL (run regsvr32.exe on the DLL). I suggest you install in "c:\program files\perforce" or wherever you installed Perforce.

An installer will be provided in the future.

Compiler

This COM DLL has been built with Visual Studio 2010.

Use either of the project settings:

To build P4COM, set up a Workspace with a View similar to the one below (note that the Perforce API and the OpenSSL libraries are pulled in via View lines):

//guest/robert_cowham/perforce/API/p4com/main/src/... //p4com-test/p4com/src/...
//guest/robert_cowham/perforce/API/p4/p4api-2014.1.821990-vs2010_static/... //p4com-test/p4api/...
//guest/robert_cowham/openssl/1.0.1j/out32dll/libeay32.dll //p4com-test/p4com/src/ReleaseMinDependency/libeay32.dll
//guest/robert_cowham/openssl/1.0.1j/out32dll/ssleay32.dll //p4com-test/p4com/src/ReleaseMinDependency/ssleay32.dll
//guest/robert_cowham/openssl/1.0.1j/out32dll/libeay32.lib //p4com-test/openssl-lib/libeay32.lib
//guest/robert_cowham/openssl/1.0.1j/out32dll/ssleay32.lib //p4com-test/openssl-lib/ssleay32.lib

Main Interface

The interface has been reworked from previous incarnations and is similar to that implemented by Tony Smith for Ruby.

There is a single object P4COM.p4 which should be instantiated and used.

Note that the command will pick up Perforce environment information in the the normal way - so global settings are used, or P4CONFIG settings are used. Make sure the current directory where your code is executing is set appropriately.

Examples

The simplest way to test is from VBScript. Note that since this can't access COM functions using String Arrays, it uses similar calls which take Variant Arrays.

Test.vbs:
---------------
Option Explicit

Dim p4, arr, str, info, errs, warns

Set p4 = WScript.CreateObject("P4COM.p4")

p4.port = "1666"
p4.client = "bruno_ws"
p4.ExceptionLevel = 0
p4.connect

DisplayResult("info")
DisplayResult("describe 704")

p4.disconnect
p4.Tagged
p4.connect
DisplayResult("describe 704")

sub PrintArray(msg, arr)
    if (UBound(arr) >= 0) then
        WScript.Echo(msg & join(arr, chr(13) & chr(10)))
    end if
end sub

sub DisplayResult(cmd)
    WScript.Echo("Cmd: " & cmd)
    PrintArray "Info: ", p4.run_variant(cmd)
    PrintArray "Warnings: ", p4.Warnings_variant
    PrintArray "Errors: ", p4.Errors_variant
end sub

See other VBScript examples: test\test_parse.vbs and test\test_submit.vbs.

As an example of using it, in a VB project, make sure you have set a reference to COM object "p4com 1.0 Type Library".

You can then execute a simple command such as "p4 info":

Dim p4 As P4COM.p4
Dim OutputArr() As String
Dim i as integer

p4.Connect
OutputArr = p4.run("info")
For i = LBound(OutputArr) To UBound(OutputArr)
    ResultBox.AddItem OutputArr(i)
Next
p4.Disconnect

This assumes a Listbox called ResultBox.

Something similar in C#:

    P4COM.p4 m_p4 = new P4COM.p4();
    System.Array output;
    System.Array warnings;
    System.Array errors;

    m_p4.Connect();
    m_p4.ExceptionLevel = 1;
    try
    {
            output = m_p4.run(txtCmd.Text);
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
            MessageBox.Show(ex.Message);
    }
    DisplayResult();

    m_p4.Disconnect();

Using Forms

It is also possible to do a Submit, getting and setting various parts of the submit form:

In C#:

    public System.Windows.Forms.CheckedListBox Files;
    System.Array m_Files;
    m_p4.ParseForms();
    m_p4.Connect();
    output = m_p4.run("change -o");
    Description.Text = m_p4.get_Var("Description"); // Simple string variable
    m_Files = m_p4.get_ArrayVar("Files"); // StringArray variable
    Files.Items.Clear();
    for (i = m_Files.GetLowerBound(0); i <= m_Files.GetUpperBound(0); i++)
    {
        Files.Items.Add(m_Files.GetValue(i));
    }

The above gets values from the submit. Now we update having allowed the user to select files and type in a description, and finish the submit:

    int i, count;
        
    m_p4.set_Var("Description", Description.Text); // Setting string var
    count = 0;
    for (i = 0; i < Files.Items.Count; i++)
    {
        if (Files.GetItemChecked(i))
        {
            count++;
        }
    }
    if (count > 0)
    {
        // There must be a better way of resetting m_Files than this
        // but I haven't found it yet!!
        string[] arr = new string[count];
        int j = 0;
        foreach(object item in Files.CheckedItems) 
        {
            arr[j++] = item.ToString();
        }
        m_Files = arr;	// Resets the array to new size
        m_p4.set_ArrayVar("Files", ref m_Files);
        output = m_p4.run("submit -i");
        errors = m_p4.Errors;

        string s = join(output);	// join output array together into one string
        s = s + "\n" + "Errors: " + join(errors);
        MessageBox.Show(s);
    }

Unicode Enabled

p4com.dll is unicode enabled and is an example of how to use the Perforce API with Unicode servers (where P4CHARSET is in use). Being COM, all strings such as filenames, change descriptions and the like, may be in Unicode and will be handled correctly - the challenge then is to be able to display the results!

Interface

Properties

The following properties may be read or updated. If you are updating them, then this must be done prior to calling Connect. If these properties are not set, then they default to system global settings (e.g. registry or environment, or P4CONFIG settings depending on the current directory).

Running commands

cmd is a simple (valid) perforce command (with quotes around embedded spaces and without leading "p4"), such as:

info
edit //depot/main/jam.c
edit -t text+w "//depot/some path with spaces/file.c"
clients

Credits

Thanks to Tony Smith who helped along the way, and whose Ruby API provided inspiration.

Changes

See CHANGELOG.txt

Top

$Id: //guest/robert_cowham/perforce/API/p4com/main/index.html#23 $