C# "Must declare a body" error

PeterFrb
PeterFrb used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Did you declared it inside a class or is it outside of all classes?
PeterFrbData anslyst, tableau visualization developer, vb certified

Author

Commented:
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 SotoRetired
Distinguished Expert 2017

Commented:
Please post the class you have the code defining CreateBindCtx and also the class in which you call this function in if different.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

PeterFrbData anslyst, tableau visualization developer, vb certified

Author

Commented:
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 SotoRetired
Distinguished Expert 2017

Commented:
Is this a Windows Form, Console or other application?
Retired
Distinguished Expert 2017
Commented:
Try adding the keyword static between the two keywords public and extern as shown below.  
   
[DllImport("ole32.dll")]
public static extern void GetRunningObjectTable(
        int reserved,
        out IRunningObjectTable prot);

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

Open in new window

PeterFrbData anslyst, tableau visualization developer, vb certified

Author

Commented:
Perfect answer.  Thank you.
Fernando SotoRetired
Distinguished Expert 2017

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial