PW API search functions from C#

Hi Guys!

Did you ever get the search to work in C#

We gave it a try and it feels like we are almost there, except we can’t get it to work Blush, we have tried a lot of different things so the code might not be the closest we got but any help to figure out where we go wrong would be greatly appreciated!!!

The SQueryCriDataBufferAddCriterion part seems to be returning something so that part might be ok, but the FindDocumentsToBuffer returns false and an empty result, with the message that the “Search execution has failed”.

Thanks in advance!

 /Per Bodin - AEC AB

Here are a code snippet (including a short snippet that does the same thing in c++ that works…) and the com calls and the structs. (We have tried classes and pointers for items such as PWGUID in the structs)

 

#region Code Blush

try

            {

                /*               

                This works fine in c++

                BOOL bCancel = FALSE;

                        hQueryCriBuffer = aaApi_SQueryCriDataBufferAddCriterion(

                                 hQueryCriBuffer, 0, UT_REGULAR, &psetattr, L"TB_TITLE_LINE_1",

                                 0, DMS_RELATION_EQUAL,

                                 AADMS_ATTRFORM_DATATYPE_STRING, L"GEMENSAMT");

                        LPRESULTCOLUMNS          lpResultColumns2 = (LPRESULTCOLUMNS)calloc(1, sizeof(DocumentProperties_stc));

                        memcpy(&lpResultColumns2->sProperty[0].guidPropertySet, &psetDocumentGeneric, sizeof(GUID));

                        lpResultColumns2->sProperty[0].lpctstrPropertyName = NULL;

                        lpResultColumns2->sProperty[0].dwPropertyID = QRY_DOC_PROP_NAME;

                        lpResultColumns2->dwPropertyCount = 1;

                        LPFINDDOC_RESULTS results2 = NULL;

                        bool ok = aaApi_FindDocumentsToBuffer(hQueryCriBuffer, lpResultColumns2, &bCancel, &results2);

                */

 

                //mk: @MSITStore: C:\Program % 20Files\Bentley\ProjectWise\_SDK\htmlhelp\PWPublicSDK.chm::/ group__aadmsapi___search_functions.html

                //If we are in a threaded envirinment:

                IntPtr lSource = PWConnection.ActivateDatasourceByHandle(SelectedProject.ProjectWiseConnection.DataSourceHandle);

                IntPtr buffer = IntPtr.Zero;

                //#define PSET_DOCUMENT_GENERIC { 0x4e43310a, 0x4524, 0x4417, { 0xae, 0x6e, 0xd7, 0x3b, 0xd7, 0x79, 0x61, 0x23 } }

                //{ 0x53dc3798, 0xa8fc, 0x415a, { 0x9e, 0xbf, 0x23, 0x70, 0xdf, 0x19, 0x20, 0x31 }

 

                PWGUID PSET_DOCUMENT_GENERIC = HelperFunctions.CreatePWGUID(0x4e43310a, 0x4524, 0x4417, new byte[8] { 0xae, 0x6e, 0xd7, 0x3b, 0xd7, 0x79, 0x61, 0x23 });

                PWGUID PSET_ATTRIBUTE_GENERIC = HelperFunctions.CreatePWGUID(0x53dc3798, 0xa8fc, 0x415a, new byte[8] { 0x9e, 0xbf, 0x23, 0x70, 0xdf, 0x19, 0x20, 0x31 });

                Guid guidparent = PWWrapper.PWStructs.HelperFunctions.GetGuid(PSET_ATTRIBUTE_GENERIC);

                IntPtr PSET_ATTRIBUTE_GENERIC_Ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PSET_ATTRIBUTE_GENERIC));

                Marshal.StructureToPtr(PSET_ATTRIBUTE_GENERIC, PSET_ATTRIBUTE_GENERIC_Ptr, true);

 

                IntPtr PSET_DOCUMENT_GENERIC_Ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PSET_DOCUMENT_GENERIC));

                Marshal.StructureToPtr(PSET_DOCUMENT_GENERIC, PSET_DOCUMENT_GENERIC_Ptr, true);

 

                PWError.RemoveAllErrors();

                buffer = PWSQuery.SQueryCriDataBufferAddCriterion(buffer, 0, (int)PWCriteriaUsageType.UT_REGULAR, PSET_ATTRIBUTE_GENERIC_Ptr, "TB_TITLE_LINE_1", 0, PWDMSRelation.DMS_RELATION_EQUAL, PWAttributeTypes.AADMS_ATTRFORM_DATATYPE_STRING, "GEMENSAMT");

                string err = PWError.GetLastErrorMessage();

                LPRESULTCOLUMNS lpres = new LPRESULTCOLUMNS();

                LPCOMPPROPREFERENCE prop = new LPCOMPPROPREFERENCE();

                prop.guidPropertySet = PSET_DOCUMENT_GENERIC_Ptr;

                prop.dwPropertyID = (uint)PWQueryParameter.QRY_DOC_PROP_NAME;

                prop.dwResultType = 0;

                prop.lpctstrPropertyName = IntPtr.Zero;

 

                //build array of pointers to LPCOMPPROPREFERENCE

                int size = 1;

                IntPtr[] InPointers = new IntPtr[size];

                int dim = IntPtr.Size * size;

                IntPtr rRoot = Marshal.AllocCoTaskMem(dim);

                //for (int i = 0; i < length; i++)

                //{

                //Just testing with one property:

                IntPtr LPCOMPPROPREFERENCE_Ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(prop));

                Marshal.StructureToPtr(prop, LPCOMPPROPREFERENCE_Ptr, true);

                LPCOMPPROPREFERENCE pwguid = (LPCOMPPROPREFERENCE)Marshal.PtrToStructure(LPCOMPPROPREFERENCE_Ptr, typeof(LPCOMPPROPREFERENCE));

                InPointers[0] = LPCOMPPROPREFERENCE_Ptr;

                //}

                //copy the array of pointers

                Marshal.Copy(InPointers, 0, rRoot, size);

                lpres.sProperty = rRoot;// new LPCOMPPROPREFERENCE[] { prop };

                lpres.dwPropertyCount = 1;

               

                FINDDOC_RESULTS results = new FINDDOC_RESULTS();

                IntPtr res = Marshal.AllocCoTaskMem(Marshal.SizeOf(results));

                Marshal.StructureToPtr(results, res, false);

 

                IntPtr LPRESULTCOLUMNS_Ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(lpres));

                Marshal.StructureToPtr(lpres, LPRESULTCOLUMNS_Ptr, true);

                bool bok = PWSearch.FindDocumentsToBuffer(buffer, LPRESULTCOLUMNS_Ptr, false, res);

                err = PWError.GetLastErrorMessage();

                bool bok2 = PWSearch.FindDocumentsToBuffer(buffer, IntPtr.Zero, false, res);

                err = PWError.GetLastErrorMessage();

            }

            catch (Exception ex)

            {

            }

 

#region API Blush

[DllImport("dmscli.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

public static IntPtr SQueryCriDataBufferAddCriterion(IntPtr hCriteiaBuf, int lOrGroup, int lFlags, IntPtr lpcPropertySet, string lpctstrPropertyName, int lPropertyId, PWDMSRelation lRelationId, PWAttributeTypes lFieldType,

string lpctstrFieldValue)

        {

            try

            {

                return aaApi_SQueryCriDataBufferAddCriterion(hCriteiaBuf, lOrGroup, lFlags, lpcPropertySet, lpctstrPropertyName, lPropertyId, (int)lRelationId, (int)lFieldType, lpctstrFieldValue);

            }

            catch (Exception)

            {

                throw;

            }

        }

[DllImport("dmscli.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

private static extern bool aaApi_FindDocumentsToBuffer(IntPtr hCriteriaBuf, IntPtr lpResultColumns, IntPtr pbCancel, IntPtr ppDataBuffer);

public static bool FindDocumentsToBuffer(IntPtr hCriteriaBuf, IntPtr lpResultColumns, bool pbCancel, IntPtr ppDataBuffer)

        {

            try

            {

                IntPtr bool_Ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(pbCancel));

                Marshal.StructureToPtr(pbCancel, bool_Ptr, true);

                return aaApi_FindDocumentsToBuffer(hCriteriaBuf,  lpResultColumns, bool_Ptr,  ppDataBuffer);

            }

            catch (Exception)

            {

                throw;

            }

        }

 

#region Structs

 

[StructLayout(LayoutKind.Explicit)]

    public struct LPRESULTCOLUMNS

    {

        //DWORD dwPropertyCount

        [FieldOffset(0)]

        public UInt32 dwPropertyCount;

        //The count of values in sProperty array.

        [FieldOffset(4)]

        //eQueryResultFlags_t  eResultFlags

        public UInt32 eResultFlags;

        //Flags describing the expected result set.

        [FieldOffset(8)]

        //CompactPropertyReference sProperty [1]

        //An array of columns requested as query results.

        public IntPtr sProperty;

    }

 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

    public struct FINDDOC_RESULTS

    {

        uint dwRowCount;

        //LPFINDDOC_RESULT pRow;

        IntPtr pRow;

        uint dwMoreResultsToFollow;

    }

 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

    //[StructLayout(LayoutKind.Explicit)]

    public struct LPCOMPPROPREFERENCE

    {

        //[FieldOffset(0)]

        public IntPtr guidPropertySet; //Kanske pointer istället

        ////[FieldOffset(16)]

        //[MarshalAs(UnmanagedType.LPWStr)]

        public IntPtr lpctstrPropertyName;

        ////[FieldOffset(28)]

        public uint dwPropertyID;

        ////[FieldOffset(32)]

        public uint dwResultType;

   }

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

    public unsafe struct PWGUID

    {

        public UInt32 Data1;

        public UInt16 Data2;

        public UInt16 Data3;

        public fixed Byte Data4[8];

    }

Parents Reply Children
No Data