Skip to content

Rewrite Type.GetTypeFromProgID in C# #42546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,18 @@ public static string GetTypeInfoName(ITypeInfo typeInfo)

// This method is identical to Type.GetTypeFromCLSID. Since it's interop specific, we expose it
// on Marshal for more consistent API surface.
[SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID(Guid clsid) => RuntimeType.GetTypeFromCLSIDImpl(clsid, null, throwOnError: false);
internal static Type? GetTypeFromCLSID(Guid clsid, string? server, bool throwOnError)
{
// Note: "throwOnError" is a vacuous parameter. Any errors due to the CLSID not being registered or the server not being found will happen
// on the Activator.CreateInstance() call. GetTypeFromCLSID() merely wraps the data in a Type object without any validation.

Type? type = null;
GetTypeFromCLSID(clsid, server, ObjectHandleOnStack.Create(ref type));
return type;
}

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern void GetTypeFromCLSID(in Guid clsid, string? server, ObjectHandleOnStack retType);

/// <summary>
/// Return the IUnknown* for an Object if the current context is the one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4085,24 +4085,6 @@ private extern object InvokeDispMethod(
bool[]? byrefModifiers, int culture, string[]? namedParameters);
#endif // FEATURE_COMINTEROP

#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Type? GetTypeFromProgIDImpl(string progID, string? server, bool throwOnError);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Type? GetTypeFromCLSIDImpl(Guid clsid, string? server, bool throwOnError);
#else // FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
internal static Type GetTypeFromProgIDImpl(string progID, string? server, bool throwOnError)
{
throw new NotImplementedException("CoreCLR_REMOVED -- Unmanaged activation removed");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling Type.GetTypeFromProgID on Unix gives this today:

Unhandled exception. System.NotImplementedException: CoreCLR_REMOVED -- Unmanaged activation removed
   at System.RuntimeType.GetTypeFromProgIDImpl(String progID, String server, Boolean throwOnError)
   at System.Type.GetTypeFromProgID(String progID)

This change is changing it to nice COM interop not support PlatformNotSupportedException.

}

internal static Type GetTypeFromCLSIDImpl(Guid clsid, string? server, bool throwOnError)
{
throw new NotImplementedException("CoreCLR_REMOVED -- Unmanaged activation removed");
}
#endif // FEATURE_COMINTEROP_UNMANAGED_ACTIVATION

#endregion

#if FEATURE_COMINTEROP
Expand Down
28 changes: 0 additions & 28 deletions src/coreclr/src/System.Private.CoreLib/src/System/Type.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,6 @@ public bool IsInterface
return TypeNameParser.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, ref stackMark);
}

////////////////////////////////////////////////////////////////////////////////
// This will return a class based upon the progID. This is provided for
// COM classic support. Program ID's are not used in COM+ because they
// have been superceded by namespace. (This routine is called this instead
// of getClass() because of the name conflict with the first method above.)
//
// param progID: the progID of the class to retrieve
// returns: the class object associated to the progID
////
[SupportedOSPlatform("windows")]
public static Type? GetTypeFromProgID(string progID, string? server, bool throwOnError)
{
return RuntimeType.GetTypeFromProgIDImpl(progID, server, throwOnError);
}

////////////////////////////////////////////////////////////////////////////////
// This will return a class based upon the CLSID. This is provided for
// COM classic support.
//
// param CLSID: the CLSID of the class to retrieve
// returns: the class object associated to the CLSID
////
[SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID(Guid clsid, string? server, bool throwOnError)
{
return RuntimeType.GetTypeFromCLSIDImpl(clsid, server, throwOnError);
}

internal virtual RuntimeTypeHandle GetTypeHandleInternal()
{
return TypeHandle;
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/src/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ FCFuncStart(gSystem_RuntimeType)
FCFuncElement("CanValueSpecialCast", ReflectionInvocation::CanValueSpecialCast)
FCFuncElement("AllocateValueType", ReflectionInvocation::AllocateValueType)
#if defined(FEATURE_COMINTEROP)
FCFuncElement("GetTypeFromCLSIDImpl", ReflectionInvocation::GetClassFromCLSID)
FCFuncElement("GetTypeFromProgIDImpl", ReflectionInvocation::GetClassFromProgID)
FCFuncElement("InvokeDispMethod", ReflectionInvocation::InvokeDispMethod)
#endif // defined(FEATURE_COMINTEROP)
FCFuncEnd()
Expand Down Expand Up @@ -794,6 +792,7 @@ FCFuncStart(gInteropMarshalFuncs)
FCFuncElement("GetTypedObjectForIUnknown", MarshalNative::GetTypedObjectForIUnknown)
FCFuncElement("ChangeWrapperHandleStrength", MarshalNative::ChangeWrapperHandleStrength)
FCFuncElement("CleanupUnusedObjectsInCurrentContext", MarshalNative::CleanupUnusedObjectsInCurrentContext)
QCFuncElement("GetTypeFromCLSID", MarshalNative::GetTypeFromCLSID)
#endif // FEATURE_COMINTEROP
FCFuncEnd()

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/src/vm/eehash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ DWORD EEClassFactoryInfoHashTableHelper::Hash(ClassFactoryInfo *pKey)

if (pKey->m_strServerName)
{
WCHAR *pSrvNameData = pKey->m_strServerName;
PCWSTR pSrvNameData = pKey->m_strServerName;

while (*pSrvNameData != 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/src/vm/eehash.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ typedef EEHashTable<const SigTypeContext*, EEInstantiationHashTableHelper, FALSE

struct ClassFactoryInfo
{
GUID m_clsid;
WCHAR *m_strServerName;
GUID m_clsid;
PCWSTR m_strServerName;
};

class EEClassFactoryInfoHashTableHelper
Expand Down
133 changes: 9 additions & 124 deletions src/coreclr/src/vm/interoputil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3772,22 +3772,20 @@ void IUInvokeDispMethod(

#if defined(FEATURE_COMINTEROP_UNMANAGED_ACTIVATION) && defined(FEATURE_COMINTEROP)

void GetComClassHelper(
static void GetComClassHelper(
_Out_ OBJECTREF *pRef,
_In_ EEClassFactoryInfoHashTable *pClassFactHash,
_In_ ClassFactoryInfo *pClassFactInfo,
_In_opt_ WCHAR *wszProgID)
_In_ ClassFactoryInfo *pClassFactInfo)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
MODE_COOPERATIVE;
INJECT_FAULT(ThrowOutOfMemory());
PRECONDITION(CheckPointer(pRef));
PRECONDITION(CheckPointer(pClassFactHash));
PRECONDITION(CheckPointer(pClassFactInfo));
PRECONDITION(CheckPointer(wszProgID, NULL_OK));
}
CONTRACTL_END;

Expand All @@ -3809,17 +3807,9 @@ void GetComClassHelper(
// represent it.
//

NewHolder<ComClassFactory> pComClsFac = ComClassFactoryCreator::Create(pClassFactInfo->m_clsid);
NewHolder<ComClassFactory> pComClsFac = new ComClassFactory(pClassFactInfo->m_clsid);
pComClsFac->SetManagedVersion();

NewArrayHolder<WCHAR> wszRefProgID = NULL;
if (wszProgID)
{
size_t len = wcslen(wszProgID)+1;
wszRefProgID = new WCHAR[len];
wcscpy_s(wszRefProgID, len, wszProgID);
}

NewArrayHolder<WCHAR> wszRefServer = NULL;
if (pClassFactInfo->m_strServerName)
{
Expand All @@ -3828,7 +3818,7 @@ void GetComClassHelper(
wcscpy_s(wszRefServer, len, pClassFactInfo->m_strServerName);
}

pComClsFac->Init(wszRefProgID, wszRefServer, NULL);
pComClsFac->Init(wszRefServer, NULL);
AllocateComClassObject(pComClsFac, pRef);

// Insert to hash.
Expand All @@ -3838,130 +3828,25 @@ void GetComClassHelper(
// Make sure the hash code is working.
_ASSERTE (pClassFactHash->GetValue(pClassFactInfo, (HashDatum *)&hRef));

wszRefProgID.SuppressRelease();
wszRefServer.SuppressRelease();
pComClsFac.SuppressRelease();
}
}

//-------------------------------------------------------------
// returns a ComClass reflect class that wraps the IClassFactory
void GetComClassFromProgID(STRINGREF srefProgID, STRINGREF srefServer, OBJECTREF *pRef)
void GetComClassFromCLSID(REFCLSID clsid, _In_opt_z_ PCWSTR wszServer, OBJECTREF *pRef)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
INJECT_FAULT(COMPlusThrowOM());
PRECONDITION(srefProgID != NULL);
PRECONDITION(pRef != NULL);
}
CONTRACTL_END;

NewArrayHolder<WCHAR> wszProgID;
NewArrayHolder<WCHAR> wszServer;
HRESULT hr = S_OK;
MethodTable* pMT = NULL;
CLSID clsid = {0};

//
// Allocate strings for the ProgID and the server.
//

int len = srefProgID->GetStringLength();

wszProgID = new WCHAR[len+1];

if (len)
memcpy(wszProgID, srefProgID->GetBuffer(), (len*2));
wszProgID[len] = W('\0');

if (srefServer != NULL)
{
len = srefServer->GetStringLength();

wszServer = new WCHAR[len+1];

if (len)
memcpy(wszServer, srefServer->GetBuffer(), (len*2));
wszServer[len] = W('\0');
}


//
// Call GetCLSIDFromProgID() to convert the ProgID to a CLSID.
//

EnsureComStarted();

{
GCX_PREEMP();
hr = GetCLSIDFromProgID(wszProgID, &clsid);
}

if (FAILED(hr))
COMPlusThrowHR(hr);

//
// See if we can find the well known managed class for this CLSID.
//

// Check if we have in the hash.
OBJECTHANDLE hRef;
ClassFactoryInfo ClassFactInfo;
ClassFactInfo.m_clsid = clsid;
ClassFactInfo.m_strServerName = wszServer;
EEClassFactoryInfoHashTable *pClassFactHash = GetAppDomain()->GetClassFactHash();

if (pClassFactHash->GetValue(&ClassFactInfo, (HashDatum *)&hRef))
{
*pRef = ObjectFromHandle(hRef);
}
else
{
GetComClassHelper(pRef, pClassFactHash, &ClassFactInfo, wszProgID);
}

// If we made it this far *pRef better be set.
_ASSERTE(*pRef != NULL);
}

//-------------------------------------------------------------
// returns a ComClass reflect class that wraps the IClassFactory
void GetComClassFromCLSID(REFCLSID clsid, STRINGREF srefServer, OBJECTREF *pRef)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
INJECT_FAULT(COMPlusThrowOM());
PRECONDITION(pRef != NULL);
}
CONTRACTL_END;

NewArrayHolder<WCHAR> wszServer;
HRESULT hr = S_OK;
MethodTable* pMT = NULL;

//
// Allocate strings for the server.
//

if (srefServer != NULL)
{
int len = srefServer->GetStringLength();

wszServer = new WCHAR[len+1];

if (len)
memcpy(wszServer, srefServer->GetBuffer(), (len*2));

wszServer[len] = W('\0');
}


//
// See if we can find the well known managed class for this CLSID.
//
Expand All @@ -3979,7 +3864,7 @@ void GetComClassFromCLSID(REFCLSID clsid, STRINGREF srefServer, OBJECTREF *pRef)
}
else
{
GetComClassHelper(pRef, pClassFactHash, &ClassFactInfo, NULL);
GetComClassHelper(pRef, pClassFactHash, &ClassFactInfo);
}

// If we made it this far *pRef better be set.
Expand Down Expand Up @@ -4027,11 +3912,11 @@ ClassFactoryBase *GetComClassFactory(MethodTable* pClassMT)
GUID guid;
pClassMT->GetGuid(&guid, TRUE);

ComClassFactory *pComClsFac = ComClassFactoryCreator::Create(guid);
ComClassFactory *pComClsFac = new ComClassFactory(guid);

pNewFactory = pComClsFac;

pComClsFac->Init(NULL, NULL, pClassMT);
pComClsFac->Init(NULL, pClassMT);

// store the class factory in EE Class
if (!pClassMT->SetComClassFactory(pNewFactory))
Expand Down
6 changes: 1 addition & 5 deletions src/coreclr/src/vm/interoputil.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,9 @@ void IUInvokeDispMethod(
#ifdef FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
// Class Factory helpers

//--------------------------------------------------------------------------
// GetComClassFromProgID used by reflection class to setup a Class based on ProgID
void GetComClassFromProgID(STRINGREF srefProgID, STRINGREF srefServer, OBJECTREF* pRef);

//--------------------------------------------------------------------------
// GetComClassFromCLSID used by reflection class to setup a Class based on CLSID
void GetComClassFromCLSID(REFCLSID clsid, STRINGREF srefServer, OBJECTREF* pRef);
void GetComClassFromCLSID(REFCLSID clsid, _In_opt_z_ PCWSTR wszServer, OBJECTREF* pRef);

//-------------------------------------------------------------
// check if a ComClassFactory has been setup for this class
Expand Down
Loading