Avatar of PeterFrb
PeterFrb
Flag 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
C#.NET Programming

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

Did you declared it inside a class or is it outside of all classes?
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
Fernando Soto

Please post the class you have the code defining CreateBindCtx and also the class in which you call this function in if different.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
PeterFrb

ASKER
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

Fernando Soto

Is this a Windows Form, Console or other application?
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PeterFrb

ASKER
Perfect answer.  Thank you.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Fernando Soto

Not a problem Peter, glad I was able to help.