Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

how to Invoke Static .Net Members from COM application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mohammad - 27 Jun 2005 21:41 GMT
Hi,

I am trying to use my .Net component from COM application.
I have some static members in my .Net component.
I tried to use reflection to invoke static members.
Any body has example of invoking static members?

Thanks
Mohammad
namespace Mcc.TestAutomation.Utilities
{
public class MyStatics
{
   static MyStatics()
   {
   }
   public static void printHelloWorld()
   {
    Console.Out.WriteLine("Hello World!");
   }
}
}
And Adam Nathan Book. But it can not call my static member
HRESULT InvokeStaticMember(BSTR typeName, BSTR memberName,
       BindingFlags memberType, SAFEARRAY* parameters, VARIANT* retval)
{
   HRESULT hresult;
   IUnknown * pUnk = NULL;
   _Object* pObj   = NULL;
   _Type* pType    = NULL;
   _Type* pTypeOfType = NULL;
   _Type* pDesiredType = NULL;

   VARIANT typeNameParam;
   VARIANT getTypeRetVal;
   VARIANT nullObject;

   SAFEARRAY* psa;
   LONG index;
   BSTR getTypeName = SysAllocString(L"GetType");

   VariantInit(&typeNameParam);
   VariantInit(&getTypeRetVal);
   VariantInit(&nullObject);

   hresult = CoCreateInstance(CLSID_Object, NULL, CLSCTX_INPROC_SERVER,
IID_IUnknown, (void **)&pUnk);
   if (FAILED(hresult)) goto cleanup;

   //Get The object Interface so we can call GetType
   hresult = pUnk->QueryInterface(IID__Object, (void **)&pObj);
   if (FAILED(hresult)) goto cleanup;

   hresult = pObj->GetType(&pType);
   if (FAILED(hresult)) goto cleanup;

   hresult = pType->GetType(&pTypeOfType);
   if (FAILED(hresult)) goto cleanup;

   psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
   typeNameParam.vt = VT_BSTR;
   typeNameParam.bstrVal = typeName;
   index =0;

   hresult = SafeArrayPutElement(psa, &index, &typeNameParam);
   if (FAILED(hresult)) goto cleanup;

   nullObject.vt = VT_EMPTY;
   hresult = pTypeOfType->InvokeMember_3(getTypeName,
       (BindingFlags)(BindingFlags_InvokeMethod | BindingFlags_Public |
       BindingFlags_Static | BindingFlags_FlattenHierarchy), NULL,
nullObject, psa, &getTypeRetVal);
   if (FAILED(hresult)) goto cleanup;

   hresult = getTypeRetVal.punkVal->QueryInterface(IID__Type, (void **)
&pDesiredType);
   if (FAILED(hresult)) goto cleanup;

   hresult = pDesiredType->InvokeMember_3(memberName,
       (BindingFlags) (memberType | BindingFlags_Public |
       BindingFlags_Static | BindingFlags_FlattenHierarchy), NULL,
       nullObject, parameters, retval);
   if (FAILED(hresult)) goto cleanup;

cleanup:
   if (pUnk) pUnk->Release();
   if (pObj) pObj->Release();
   if (pType) pType->Release();
   if (pTypeOfType) pTypeOfType->Release();
   if (pDesiredType) pDesiredType->Release();
   if (getTypeName) SysFreeString(getTypeName);
   SafeArrayDestroy(psa);
   VariantClear(&typeNameParam);
   VariantClear(&getTypeRetVal);
   VariantClear(&nullObject);

   return hresult;
}
mohammad - 28 Jun 2005 22:23 GMT
Here is the solution.

   IUnknown*   pUnk  = NULL;
   IDispatch*  pDisp = NULL;
   HRESULT hresult;
   BSTR memberName;
   _Type* pType    = NULL;
   _Type* pDesiredType = NULL;
   VARIANT nullObject;
   VARIANT retVal;
   VariantInit(&nullObject);
   VariantInit(&retVal);
   nullObject.vt = VT_EMPTY;
   _Object* pObj   = NULL;

   //initialize COM
   hresult = CoInitialize(NULL);
   if (FAILED(hresult))
   {
       printf("ERROR: Cannot initialize COM 0x%x\n", hresult);
       return -1;
   }

   hresult = CoCreateInstance(CLSID_MyStatics, NULL, CLSCTX_INPROC_SERVER,
IID_IUnknown, (void **)&pUnk);
   if (FAILED(hresult))
   {
       printf("ERROR: Cannot create object 0x%x\n", hresult);
       return -1;
   }
   //Get The object Interface so we can call GetType
   hresult = pUnk->QueryInterface(IID__MyStatics, (void **)&pObj);
   if (FAILED(hresult))
   {
       printf("ERROR: Cannot obtain IDispatch interface pointer 0x%x\n",
hresult);
       return -1;
   }
   hresult = pObj->GetType(&pType);
   if (FAILED(hresult))
   {
       printf("ERROR: Cannot obtain IDispatch interface pointer 0x%x\n",
hresult);
       return -1;
   }
   memberName = SysAllocString(L"printHelloWorld");

   hresult = pType->InvokeMember_3(memberName,
       (BindingFlags) (BindingFlags_InvokeMethod | BindingFlags_Public |
       BindingFlags_Static | BindingFlags_FlattenHierarchy), NULL,
       nullObject, NULL, &retVal);
   if (FAILED(hresult))
   {
       printf("ERROR: Cannot obtain IDispatch interface pointer 0x%x\n",
hresult);
       return -1;
   }
   CoUninitialize();

>Hi,
>
[quoted text clipped - 94 lines]
>    return hresult;
>}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.