Link to home
Start Free TrialLog in
Avatar of PeterFrb
PeterFrbFlag for United States of America

asked on

C# "Must declare a body" error

I am new to C#, and I don't understand how to resolve this issue:

        [DllImport("ole32.dll")]
        public extern int CreateBindCtx(
            uint reserved,
            out IBindCtx ppbc);

Open in new window


The code block comes up with the following error:

'RunningObjectTable.Program.CreateBindCtx(uint, out System.Runtime.InteropServices.ComTypes.IBindCtx)' must declare a body because it is not marked abstract, extern, or partial

Open in new window


I'm not at all clear how to give a declared function a "body".  Also, excuse me, but it is so labeled "extern"!  I also have another declared function as "public extern void", which does not return this error, indicating that my syntax on the function above is correct.  Some basic info on this is appreciated.  Thanks!  ~Peter Ferber
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Did you declared it inside a class or is it outside of all classes?
Avatar of PeterFrb

ASKER

Inside the class.  When I move it outside the class, the error changes:

"int" is highlighted, and the error message is "Expected class, delegate, enum, interface, or struct"

Also, in the code where the function is called, I get the following error: "The name 'CreateBindCtx' does not exist in the current context".  

When I declare it outside the namespace, I come up with the same errors.

Thanks, ~Peter
Please post the class you have the code defining CreateBindCtx and also the class in which you call this function in if different.
Gladly.  I received this from the internet, and nothing about it is proprietary.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using RunningObjectTable;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;


namespace RunningObjectTable
{
    class Program
    {

        [DllImport("ole32.dll")]
        public extern void GetRunningObjectTable(
            int reserved,
            out IRunningObjectTable prot);

        [DllImport("ole32.dll")]
        public extern int CreateBindCtx(
            uint reserved,
            out IBindCtx ppbc);
        
        static void Main()
        {
            RunningObjectTable.Program NewItem = new RunningObjectTable.Program();
            NewItem.Testing();
        }

        // Requires Using System.Runtime.InteropServices.ComTypes
        // Get all running instance by querying ROT
        public List<object> GetRunningInstances(string[] progIds)
        {
            List<string> clsIds = new List<string>();

            // get the app clsid
            foreach (string progId in progIds)
            {
                Type type = Type.GetTypeFromProgID(progId);
                if (type != null)
                    clsIds.Add(type.GUID.ToString().ToUpper());
            }

            // get Running Object Table ...
            IRunningObjectTable Rot = null;
            GetRunningObjectTable(0, out Rot);
            if (Rot == null)
                return null;

            // get enumerator for ROT entries
            IEnumMoniker monikerEnumerator = null;
            Rot.EnumRunning(out monikerEnumerator);
            if (monikerEnumerator == null)
                return null;
            monikerEnumerator.Reset();


            List<object> instances = new List<object>();



            IntPtr pNumFetched = new IntPtr();

            IMoniker[] monikers = new IMoniker[1];



            // go through all entries and identifies app instances
            while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)
            {
                IBindCtx bindCtx;
                CreateBindCtx(0, out bindCtx);
                if (bindCtx == null)
                    continue;
                string displayName;
                monikers[0].GetDisplayName(bindCtx, null, out displayName);

                foreach (string clsId in clsIds)
                {
                    if (displayName.ToUpper().IndexOf(clsId) > 0)
                    {
                        object ComObject;
                        Rot.GetObject(monikers[0], out ComObject);
                        if (ComObject == null)
                            continue;
                        instances.Add(ComObject);
                        break;
                    }
                }
            }
            return instances;
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public void Testing()
        {
            // Look for acad 2009 & 2010 & 2014
            string[] progIds = {"Access.Application"};
            List<object> instances = GetRunningInstances(progIds);
            foreach (object acadObj in instances)
            {
                try
                {
                    // do some stuff ...  
                }
                catch
                {


                }

            }
        }

    }
}

Open in new window

Is this a Windows Form, Console or other application?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect answer.  Thank you.
Not a problem Peter, glad I was able to help.