Re: access violation With dll?

2015-07-16 Thread jklp via Digitalmars-d-learn

On Thursday, 16 July 2015 at 17:22:50 UTC, jklp wrote:
On Thursday, 16 July 2015 at 17:04:09 UTC, Taylor Hillegeist 
wrote:

[...]


Your proto is wrong. Your forgot the FFI convention (__cdecl = 
extern(C)).

Try this instead:

---
extern(C)
alias Proto = void function(char*, int);

Proto func = cast(Proto) GetProcAddress(h, Test);

if (func)
{
char[] STUFF;
STUFF.length = 5000;
func (STUFF.ptr , STUFF.length);
// prints etc...
}



Also i guess that the dll returns a null terminated string so the 
result has to be

read like this:

---
import std.string;
printf(%s\n, fromStringz(STUFF.ptr));
---

Otherwise you'll get a random output after the 'HELLO WORLD'.
(wait maybe the console will automatically cut... ?)


Re: access violation With dll?

2015-07-16 Thread jklp via Digitalmars-d-learn
On Thursday, 16 July 2015 at 17:04:09 UTC, Taylor Hillegeist 
wrote:
Beleive it or not the code below does work. However I get an 
access violation after every run? any Ideas why?



+Code to Run DLL 
function+++


import core.runtime;
import std.stdio;
import core.memory;
import std.c.windows.windows;

int main()
{
HMODULE h;
FARPROC fp;

printf(Start Dynamic Link...\n);
h = cast(HMODULE) Runtime.loadLibrary(SharedLib.dll);

void function(ref char[], int) Testf
  = cast(void function(ref char[], int))
  GetProcAddress(h, Test); //Function Says HELLO WORLD

char[] STUFF;
STUFF.length = 5000;
Testf( STUFF , STUFF.length);

printf(%s\n, (STUFF)); //PRINTS HELLO WORLD

Runtime.unloadLibrary(h);

printf(End...\n);
return 0;
}
++END 
CODE+++

the function header has this line:

void __cdecl Test(char MyOutput[], int32_t len);

and i get this Error:
object.Error@(0): Access Violation

0x77206568
0x646C726F
0x00405A2C in int object.ModuleInfo.opApply(scope int 
delegate(object.ModuleInfo*))

0x0040222C in main
0x00414949 in mainCRTStartup
0x7678337A in BaseThreadInitThunk
0x770592E2 in RtlInitializeExceptionChain
0x770592B5 in RtlInitializeExceptionChain

To be honest I was surprised this worked, since i am fairly 
unfamiliar to linking to dlls.


Your proto is wrong. Your forgot the FFI convention (__cdecl = 
extern(C)).

Try this instead:

---
extern(C)
alias Proto = void function(char*, int);

Proto func = cast(Proto) GetProcAddress(h, Test);

if (func)
{
char[] STUFF;
STUFF.length = 5000;
func (STUFF.ptr , STUFF.length);
// prints etc...
}




Re: Defining constant values in struct

2015-06-16 Thread jklp via Digitalmars-d-learn

On Tuesday, 16 June 2015 at 21:17:37 UTC, tcak wrote:
As far as I known, when I define a string with enum and it is 
used at different parts of code, that string is repeated again 
and again in executable file instead of passing a pointer to 
string. So, using enum with string doesn't seem like a good 
idea.


Hence, I defined string as const to make it belong to struct 
itself instead of instances, but it comes with 'need `this` for 
...' error. This indicates that the string doesn't belong to 
struct itself actually.


Because there is nothing like namespace in D, I used a 
sub-struct.


[code]
struct TableSchema{
const string TABLE = users;

struct FieldTypes{
const string ID = BIGINT;
}

const string CREATESQL = ... id  ~ FieldTypes.ID ~ ...;
}
[/code]


But compiler doesn't allow me to access FieldTypes.ID. It says 
that it needs `this`. I tried with `static shared`, used 
`class` instead of `struct` etc. But couldn't have come up with 
a nice solution.


Do i miss a detail in your requirement ?

---
struct TableSchema{
const string TABLE = users;

struct FieldTypes{
static const string ID = BIGINT;
}

const string CREATESQL = ... id  ~ FieldTypes.ID ~ ...;
}
---

because this works.




Re: Writeln does not prints if array has more than 500 elements

2015-06-10 Thread jklp via Digitalmars-d-learn

On Wednesday, 10 June 2015 at 21:49:56 UTC, kerdemdemir wrote:

== NOTHÄ°NG PRINTS

What am I doing wrong?


Which OS, which terminal ?




Re: problem with gc?

2015-05-27 Thread jklp via Digitalmars-d-learn

On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:

I am writing a echoclient, as below:

Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
for(int i=0; iN; i++)
{
scope string str = format(%s,i);
conn.write((cast(ubyte*)str.ptr)[0..str.length]);
conn.read(buf[0..str.length]);
n++;
}
conn.close();


When it loops about more than 10 times,the throughput will 
fall from 6request/second to 26request/second.


If the code changes a little as below:

 scope string str = format(%s,1); //changes to a constant

It will runs normally with speed of 6request/second. I test 
for 13minutes, everything seems well.




What happened when the code changes a little? Who will give an 
explaination,Thanks a lot?


Have you tried to declare str in the upper scope ?

---
Ptr!Conn conn = connect(127.0.0.1,8881);
ubyte[100] buf;
string str;
for(int i=0; iN; i++)
{
str = format(%s,i);
conn.write(cast(ubyte[]) str);
conn.read(buf[0..str.length]);
n++;
}
conn.close();
---

And also you could try to surround the whole block with 
`GC.disable` and `GC.enable`. This would help to determine if the 
GC is involved:


---
Ptr!Conn conn = connect(127.0.0.1,8881);
GC.disable;
ubyte[100] buf;
string str;
for(int i=0; iN; i++)
{
str = format(%s,i);
conn.write(cast(ubyte[]) str);
conn.read(buf[0..str.length]);
n++;
}
GC.enable;
conn.close();
---


Re: Template type deduction and specialization

2015-05-20 Thread jklp via Digitalmars-d-learn

On Wednesday, 20 May 2015 at 06:31:13 UTC, Mike Parker wrote:
I don't understand why this behaves as it does. Given the 
following two templates:


```
void printVal(T)(T t) {
writeln(t);
}
void printVal(T : T*)(T* t) {
writeln(*t);
}
```

I find that I actually have to explicitly instantiate the 
template with a pointer type to get the specialization.


```
void main() {
int x = 100;
printVal(x);
int* px = x;
printVal(px);// prints the address
printVal!(int*)(px)  // prints 100
}
```

Intuitively, I would expect the specialization to be deduced 
without explicit instantiation. Assuming this isn't a bug (I've 
been unable to turn up anything in Bugzilla), could someone in 
the know explain the rationale behind this?


---
import std.stdio;

void printVal(T)(T t) {
writeln(t);
}

void printVal(T: T)(T* t) {
writeln(*t);
}

void main() {
int x = 100;
printVal(x);
int* px = x;
printVal(px);
}
---

here it's selected correctly without explicit instantiation. But 
honestly i don't know why since the  asterisk is removed from the 
T it looks quite incorrect.




Re: Why is one d file compiled into two files object file executable.

2015-02-11 Thread jklp via Digitalmars-d-learn
On Wednesday, 11 February 2015 at 05:08:16 UTC, Venkat Akkineni 
wrote:

Hi

I am coming from Java. What is the purpose of an object file  
why is it generated at compile time in addition to an 
executable. I know C generates an object file too, but I don't 
know what the use is.


Please point me to any detailed documentation u may have 
regarding object files.


Also does D have any facilities for dynamic code generation  
compilation ?


Thankyou
Sincerely
Venkat


It's not D-specific, every compiler for a compiled language 
produces objects:


http://en.wikipedia.org/wiki/Object_file



Re: getting all children classes in program

2015-01-03 Thread jklp via Digitalmars-d-learn

Off Topic ! But in the same way:

---
static string[] IDs;

ptrdiff_t getClassID(ClassType, ClassBase)()
if ((is(ClassType == class))  (is(ClassBase == class)))
{
import std.algorithm;
if (!is(ClassType : ClassBase))
return -1;
else {
auto classTypeString = ClassType.stringof;
ptrdiff_t result = countUntil(IDs, classTypeString);
if (result == -1) {
IDs ~= classTypeString;
result = IDs.length -1;
}
return result;
}
}

void main(string[] args)
{
class Oops {}
class A : Oops{}
class B : Oops{}
class C : Oops{}
class D : Oops{}
class E {}

assert(getClassID!(A,Oops) == 0);
assert(getClassID!(B,Oops) == 1);
assert(getClassID!(C,Oops) == 2);
assert(getClassID!(D,Oops) == 3);
assert(getClassID!(C,Oops) == 2);
assert(getClassID!(B,Oops) == 1);
assert(getClassID!(A,Oops) == 0);
assert(getClassID!(E,Oops) == -1);
}
---


Re: Idea/request: If you have a DUB project, add a code.dlang.org badge to README

2014-12-30 Thread jklp via Digitalmars-d-learn

On Tuesday, 30 December 2014 at 21:12:38 UTC, Kiith-Sa wrote:
A few weeks/months ago someone here mentioned that it'd be good 
if DUB projects linked to code.dlang.org to help anyone who 
runs into such a project quickly discover other D projects.


MAny GitHub projects have badges/shields on top of their 
READMEs - little image strips showing things like continuous 
integration status, code coverage, etc.


There's also a service generating these: shields.io

I generated a simple listed at| code.dlang.org shield, and 
added it to my project READMEs as a link pointing to 
code.dlang.org for example, see D:YAML README:


  https://github.com/kiith-sa/D-YAML

You can do the same by either linking to or downloading the 
shield:


  
https://img.shields.io/badge/listed%20at-code.dlang.org-red.png


(used red... because mars)

and putting the image (whether as a link to shields.io or your 
own copy) into your README.




It's not likely to be a huge improvement, but I expect it *can* 
help people notice more D projects and it's trivial to do.


red is connoted negative/agressive, I think blue would be better.
Or maybe yellow-mustard for those who have doubtful tastes...



Re: Idea/request: If you have a DUB project, add a code.dlang.org badge to README

2014-12-30 Thread jklp via Digitalmars-d-learn

On Tuesday, 30 December 2014 at 21:23:53 UTC, Kiith-Sa wrote:

On Tuesday, 30 December 2014 at 21:19:52 UTC, jklp wrote:

On Tuesday, 30 December 2014 at 21:12:38 UTC, Kiith-Sa wrote:
A few weeks/months ago someone here mentioned that it'd be 
good if DUB projects linked to code.dlang.org to help anyone 
who runs into such a project quickly discover other D 
projects.


MAny GitHub projects have badges/shields on top of their 
READMEs - little image strips showing things like continuous 
integration status, code coverage, etc.


There's also a service generating these: shields.io

I generated a simple listed at| code.dlang.org shield, and 
added it to my project READMEs as a link pointing to 
code.dlang.org for example, see D:YAML README:


https://github.com/kiith-sa/D-YAML

You can do the same by either linking to or downloading the 
shield:



https://img.shields.io/badge/listed%20at-code.dlang.org-red.png

(used red... because mars)

and putting the image (whether as a link to shields.io or 
your own copy) into your README.




It's not likely to be a huge improvement, but I expect it 
*can* help people notice more D projects and it's trivial to 
do.


red is connoted negative/agressive, I think blue would be 
better.

Or maybe yellow-mustard for those who have doubtful tastes...


If you want blue, just replace red with blue. I used red 
because it's the color of the D logo, site and the color of 
Mars (as D was originally called Mars and lot of D things are 
named after Mars, e.g. Phobos/Deimos etc.)


Sorry it was a trivial and useless answer. forgot that this board 
is excusively serious.