Click here to Skip to main content
12,449,440 members (60,724 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C#
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:

#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 "):
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 17-Sep-12 19:09pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Remove the
#using <mscorlib.dll>
clause.
  Permalink  
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 .

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100