2

I have 2 DLLs. I want create instance of class in my second DLL that I declare at .idl file of first library. But I get Error Link 2001 on CLSID, when try to link second DLL. I'm linking with first DLL, so I don't know why I'm getting this error.

Stock.idl

Declaration of class

[uuid(61AF2A3E-8B04-4161-B978-5776BFDF7DB5), helpstring("StockObjectManager Class")]
coclass StockObjectManager
{
    [default] interface IStockObjectManager;
    interface ISQLBase;
};

Generated Files:

Stock.h

EXTERN_C const CLSID CLSID_MazStockObjectManager;

Stock_i.c

MIDL_DEFINE_GUID(CLSID, CLSID_StockObjectManager,0x61AF2A3E,0x8B04,0x4161,0xB9,0x78,0x57,0x76,0xBF,0xDF,0x7D,0xB5);

FIleFromSecondDll.cpp

CComPtr<IStockObjectManager> m_SOManager;
m_SOManager.CoCreateInstance(CLSID_StockObjectManager); // getting Error Link 2001 unresolved external symbol "_CLSID_StockObjectManager"

I want to understand why I'm getting this error and fix it. Of course, I can just #include "Stock_i.c", but it isn't good solution, I guess.

7
  • In any linked project (exe or dll) you should have exactly one definition of CLSID_StockObjectManager, so yes you should #include "Stock_i.c", but only in one cpp per project. It's true that c filea should not be #included in general. But COM interface generated c file (..._i.c) with the classid is one exception. Commented Jun 20 at 6:48
  • Do not use generated header file from midl. Make your interfaces oleatomation compatible and use importlib + __uuidof(LibraryNamespace::CoClass), this requires no linking at the client side. Commented Jun 20 at 6:55
  • @wohlstad, also I have some DLLs. that's not mine, and there are IDL's generated files and if I link with that extern DLL, I don't need to include there _i.c file. And if I want to use some classes of Windows, I also don't need to include _i.c files. Maybe they somehow export CLSID to .lib file? Commented Jun 20 at 7:14
  • You will need to link with the _i.c file to make the constant known in your code Commented Jun 20 at 7:15
  • @PepijnKramer, I am new to WINAPI, so I have some questions. If I can use uuifof, then whats point of CLSIDFromProgID? Or it is using when I dont have definiton of class that I want to use? And why it's bad idea to use generated header file? Commented Jun 20 at 7:18

1 Answer 1

1

in Stock.h must be not only

EXTERN_C const CLSID CLSID_MazStockObjectManager;

but also

#ifdef __cplusplus
class DECLSPEC_UUID("61AF2A3E-8B04-4161-B978-5776BFDF7DB5") MazStockObjectManager;
#endif

so and use

CComPtr<IStockObjectManager> m_SOManager;
m_SOManager.CoCreateInstance(__uuidof(MazStockObjectManager));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.