Click here to Skip to main content
15,886,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a dll file to call the functions in it, it contains a CreateLink() function which is used to create shortcut file for exe. This is link of article: Creating and Resolving shell links[^], that is a class, i want to convert it to Win32API, i did as follows:

C++
#include "stdafx.h"
#using <mscorlib.dll>
#include <Shlobj.h>
#include <crtdbg.h>
#include <atldef.h>
#include <atlconv.h>

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}



HRESULT _CreateLink(LPCWSTR FilePath, LPCWSTR LnkPath, LPCWSTR LnkDesc,LPCWSTR WorkDir) 
{ 
    USES_CONVERSION;
    CoInitialize(NULL);
    IShellLink* psl;             
    HRESULT  hres = CoCreateInstance(CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); 
    if (SUCCEEDED(hres)) 
    { 
        IPersistFile* ppf; 
         
        psl->SetPath(W2A(FilePath)); 
        psl->SetWorkingDirectory(W2A(WorkDir));
        psl->SetDescription(W2A(LnkDesc)); 
            
        hres = psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf); 
 
        if (SUCCEEDED(hres)) 
        {    
            hres = ppf->Save(LnkPath, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    }
    CoUninitialize();
    return hres;
}

bool _stdcall CreateLink(LPCWSTR FilePath, LPCWSTR LnkPath, LPCWSTR LnkDesc,LPCWSTR WorkDir)
{
    IntPtr strfilepath = Marshal::StringToCoTaskMemUni(FilePath);
    IntPtr strlnkpath = Marshal::StringToCoTaskMemUni(LnkPath);
    IntPtr strlnkdesc = Marshal::StringToCoTaskMemUni(LnkDesc);
    IntPtr strworkdir = Marshal::StringToCoTaskMemUni(WorkDir);
    
    HRESULT hres = _CreateLink((LPCWSTR)strfilepath.ToPointer(),
                               (LPCWSTR)strlnkpath.ToPointer(),
                               (LPCWSTR)strlnkdesc.ToPointer(),
                               (LPCWSTR)strworkdir.ToPointer());

    Marshal::FreeCoTaskMem(strfilepath);
    Marshal::FreeCoTaskMem(strlnkpath);
    Marshal::FreeCoTaskMem(strlnkdesc);
    Marshal::FreeCoTaskMem(strworkdir);

    return (hres==S_OK);
}


When i compiled it, it generated an error (at line "#using <mscorlib.dll>"):
Error	1 fatal error C1190: managed targeted code requires a '/clr' option	c:\Users\Peter\Desktop\abc\abc\dllmain.cpp	3	abc


I searched that error, but I couldnt solve it. Please help me. Thanks.

P/s: I use VS2008 to code.
Posted

1 solution

Remove the
XML
#using <mscorlib.dll>

clause.
 
Share this answer
 
Comments
Andrewpeter 21-Sep-12 3:57am    
Thank you, but if i remove that clause it'll generate other errors, this is not a method to solve my problem, you try!
YvesDaoust 21-Sep-12 4:40am    
Have a look at those new errors and fix them. In the first place, include <windows.h>.

No I am not trying that for you.
Andrewpeter 21-Sep-12 4:47am    
Thank you very much, all those errors depend on mscorlib.dll, if i dont use it i cant fix those errors (44 errors). I think my problem is not important, i will do it next time. Thank you very much for your ideas.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900