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

p4com is contributed by Robert Cowham of Vaccaperna Systems Ltd.

See the paper I presented 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 similar to p4vb which I wrote and which is part of P4OFC - the integration between Microsoft Office and Perforce and thus is stable and tested.

Compiler

This COM DLL has been built with MS VC++ 6.0 (SP5) and compiles happily with VC++ 7.0 (VS.NET).

Use either of the project settings:

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

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.

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 P4COMLib.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

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 P4COMLib.p4
Dim OutputArr() As String
Dim i as integer

p4.Connect
OutputArr = m_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#:

    P4COMLib.p4 m_p4 = new P4COMLib.p4();
    System.Array output;
    System.Array warnings;
    System.Array errors;
		
    m_p4.Connect();
    m_p4.ExceptionLevel = 1;
    try
    {
            output = m_p4.get_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.get_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 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;

        m_p4.set_ArrayVar("Files", ref m_Files);
        output = m_p4.get_run("submit -i");
        errors = m_p4.Errors;

        string s = join(output);
        s = s + "\n" + "Errors: " + join(errors);
        MessageBox.Show(s);
    }

Example Projects

These are to be found in the zip file.

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 3 example projects (very similar functionality):

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

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.

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.

Top