a question about GetSharedLibrary

Apprentice
Posts: 19
Joined: 2007.07
Post: #1
I want to use a dylib file in my code.i wrote my code like below:

Code:
#include <string.h>
#include <CodeFragments.h>

BOOL CMacLib::LoadLib(FZ_PCSTR MyDylibFileName)
{
      BOOL bSuccess = FALSE;
      int nLen = strlen(MyDylibFileName);
      char szPLibName[256];
      szPLibName[0] = nLen;
      memcpy(szPLibName+1, MyDylibFileName, nLen);

      OSErr myerr;
      CFragArchitecture archType = kPowerPCCFragArch;
      CFragLoadOptions findFlags = kReferenceCFrag;
      CFragConnectionID connID;
      Ptr mainAddr;
      Str255 errMessage;
    
      myerr= GetSharedLibrary((ConstStr63Param)szPLibName, archType,
    findFlags, &connID, &mainAddr, errMessage);
      if(myerr== noErr)
      {
          bSuccess = TRUE;
      }
      return bSuccess;
}

Code is very simple, I built it successfully. But, when running the function "GetSharedLibrary", it returns Error: "unimpErr". Why?I can't find out the reason.So, please help me, i will thank u very much.Cry

my code is a command-line utility with C++ tools. I added the carbon framework in it.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
GetSharedLibrary is for CFM. You need to use CFBundle or NSBundle to dynamically load Mach-O code.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.07
Post: #3
Thank you, but what is CFM? My dylib file was written by c++, i can't use GetSharedLibrary to load it??? Can i use the function "dlopen" to load it ?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #4
CFM was the old binary format, Mach-O is the new and shiny one. Although I would much recommend CFBundle or NSBundle over writing dylibs, I think dlopen should be able to load it, yes. Smile
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.07
Post: #5
There is a error in the code. I did not find it.

Now, remove the line "szPLibName[0] = nLen;"
then correct the next line as
"memcpy(szPLibName, MyDylibFileName, nLen); "

But after corrected, the problem is still here.Why?
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #6
Because GetSharedLibrary doesn't work on the new Mach-O binaries. GetSharedLibrary will only work if you're building an old Mac OS 9 program. (With exceptions)
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.07
Post: #7
To Fenris,
thank you very much, I see.
I tried "dlopen", but it must use the absolut path, or set the environment parameters for path. i need put the dylib files with my execution file. I can not set a dylib name in dlopen to load it??
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Why don't you know where your library is?
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.07
Post: #9
i just know that the dylib file and the execution file will be put in the same work directory, but the path of directory can be changed. can i only set the dylib file's name in dlopen?? i think maybe it can not find the file which will be loaded if i only write the name, not path.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
you can use CFBundle to find your app's directory, then dlopen or CFBundle to load the dylib.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #11
Is dlopen available in OS X these days? Back in the 10.2 days I used "dlcompat" to get dlopen functionality.

Anyway, CFBundle is your friend. I've implemented C++ classloading by using CFBundle to look up factory functions in my executable as well as bundled plugins and it works like a charm, on both intel and PPC. No need to set architecture flags or any other malarky. It really works, and it works really well.

I can post code if you have any questions.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
dlopen is available, and has been for a while (anyone know if it's there in 10.3 or just 10.4?). The old NeXTStep stuff is deprecated in its favor, even.

The comment in dlfcn.h says it's based on dlcompat.
Quote this message in a reply
Member
Posts: 440
Joined: 2002.09
Post: #13
OneSadCookie Wrote:dlopen is available, and has been for a while (anyone know if it's there in 10.3 or just 10.4?).

>This doc< says dlopen works in 10.3 but is "more efficient" in 10.4 (for whatever that's worth).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
Looking at the source, it got radically overhauled in 10.4.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.07
Post: #15
Thank u very much, everybody. Finally i used dlopen in my code. It works well. But i still want to know how CBundle works.So, if OK, would you like to post the codes here? Thank you again!
Quote this message in a reply
Post Reply