iDevGames Forums
a question about GetSharedLibrary - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: a question about GetSharedLibrary (/thread-3093.html)



a question about GetSharedLibrary - sakiel - Aug 14, 2007 08:21 PM

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.


a question about GetSharedLibrary - OneSadCookie - Aug 14, 2007 09:21 PM

GetSharedLibrary is for CFM. You need to use CFBundle or NSBundle to dynamically load Mach-O code.


a question about GetSharedLibrary - sakiel - Aug 14, 2007 10:47 PM

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 ?


a question about GetSharedLibrary - Fenris - Aug 14, 2007 11:55 PM

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


a question about GetSharedLibrary - sakiel - Aug 15, 2007 12:39 AM

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?


a question about GetSharedLibrary - Fenris - Aug 15, 2007 12:59 AM

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)


a question about GetSharedLibrary - sakiel - Aug 15, 2007 01:12 AM

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??


a question about GetSharedLibrary - OneSadCookie - Aug 15, 2007 01:38 AM

Why don't you know where your library is?


a question about GetSharedLibrary - sakiel - Aug 15, 2007 01:55 AM

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.


a question about GetSharedLibrary - OneSadCookie - Aug 15, 2007 03:44 AM

you can use CFBundle to find your app's directory, then dlopen or CFBundle to load the dylib.


a question about GetSharedLibrary - TomorrowPlusX - Aug 15, 2007 04:41 AM

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.


a question about GetSharedLibrary - OneSadCookie - Aug 15, 2007 05:03 AM

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.


a question about GetSharedLibrary - Frank C. - Aug 15, 2007 12:19 PM

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).


a question about GetSharedLibrary - OneSadCookie - Aug 15, 2007 02:59 PM

Looking at the source, it got radically overhauled in 10.4.


a question about GetSharedLibrary - sakiel - Aug 15, 2007 08:31 PM

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!