Retrieving a string from a c file

Archives Forums/Win32 Discussion/Retrieving a string from a c file

Odds On(Posted 2005) [#1]
I'm trying to Import a C file and read in a string value from inside it using Extern. It works fine with Integer's etc but when I try and use a string I just get an Unhandled Memory Exception Error.

I threw a simple example together to show what I mean:

test.bmx
Import "test.c"

Extern

Global myString$ = "myString"

End Extern

DebugLog myString


test.c
char myString[] = "testing";


Thanks,
Chris


Perturbatio(Posted 2005) [#2]
try doing
Global myString$ = FromCString(MyString)



Odds On(Posted 2005) [#3]
Thanks Perturbatio, not working 100% yet though. If I modify the code to this:

Import "test.cpp"

Extern

Global myString:Byte Ptr = "myString"

End Extern

DebugLog String.FromCString( myString )

Then it works fine, but I'd rather it held the correct value automatically so I don't have to call FromCString( ) every time, or even once to update the value.

I can't appear to use the FromCString( ) method inside the Extern block so I'm not sure if this is possible or not.

Regards,
Chris


Perturbatio(Posted 2005) [#4]
How about:
Import "test.cpp"

Global myString:String

Extern

Global myCString:Byte Ptr = "myString"

End Extern

myString.FromCString(myCString)

DebugLog myString



Odds On(Posted 2005) [#5]
I'd rather not because then I have to call myString.FromCString(myCString) every time the value changes. For example:

' create an open file dialog and allow user to select a file
result = fuiOpenDialog(  )

' function succeeded
if result = true
     DebugLog currentFile$
endif

fuiOpenDialog( ) would be an Extern Function, and in the C file where the real fuiOpenDialog( ) function is called it assigns the selected filename to a variable called currentFile.

Hope that makes sense, all I'm basically saying is that the value will be changing all the time and it would be nice to just be able to use the string which is directly linked to the equivilant C variable.

I ploughed through all the blitz source code looking for a solution and noticed that the "blitz.mod" actually uses a series of BBClass, BBString and BBChar structs and typedefs to store strings which explains why it won't let me easily retrieve a string, but I don't really feel like including all those header files etc just so I can use the BBString struct to create my strings :P

Regards,
Chris


teamonkey(Posted 2005) [#6]
I also have this problem.

http://blitzbasic.com/Community/posts.php?topic=42316

Looking at some of the other modules I think you should be able do something like this
import "test.c"

Extern
    Function XXfuiOpenDialog:Byte Ptr() = "fuiOpenDialog()"
End Extern

Function fuiOpenDialog:String()
    return String.fromCString(XXfuiOpenDialog())
End Function


But I can't seem to get it working.


Odds On(Posted 2005) [#7]
Hi teamonkey,

To do it with a function the way you want you do this:

code.c
char *myFunction(  ) {
    return "this";
}

code.bmx
Import "code.c"

Extern

Function myFunction$z(  )

End Extern

Then just call the function to get the string..

DebugLog myFunction(  )

I don't really know why it works ok with functions but not variables.. I'll have to look into it a bit more!

Regards,
Chris


Chris C(Posted 2005) [#8]
in c

extern "C" int bb_icanseeyou=808;
...


in max

print icanseeyou

prints 808